weak_ptr 是一种不控制对象生命周期的智能指针, 它指向一个 shared_ptr 管理的对象.
进行该对象的内存管理的是那个强引用的 shared_ptr.
weak_ptr只是提供了对管理对象的一个访问手段.
为了解决std::shared_ptr在相互引用的情况下出现的问题而存在的,
std::shared_ptr赋值给weak_ptr时,weak_ptr 支持拷贝或赋值,不会引起智能指针计数增加。
weak_ptr.lock() 获取所管理的对象的强引用(shared_ptr)
weak_ptr.use_count() 返回与 shared_ptr 共享的对象的引用计数.
weak_ptr.reset() 将 weak_ptr 置空
#include <cstdio>
#include <iostream>
#include <memory> using namespace std; struct Child;
typedef std::weak_ptr<Child> STChildWeakPtr; struct Father : public std::enable_shared_from_this<Father>
{
string say;
STChildWeakPtr ChildWeakPtr;
};
typedef std::weak_ptr<Father> STFatherWeakPtr; struct Child : public std::enable_shared_from_this<Child>
{
string say;
STFatherWeakPtr FathWeakPtr;
}; int main()
{
//创建zh指针
std::shared_ptr<Father> fa(new Father);
fa->say = "Iam father";
std::shared_ptr<Child> ch(new Child);
ch->say = "Iam Child"; //赋值给weak_ptr
fa->ChildWeakPtr = ch;
ch->FathWeakPtr = fa; //通过weak_ptr获取指针对象
std::shared_ptr<Father> ff = ch->FathWeakPtr.lock();
cout << ff->say <<endl; std::shared_ptr<Child> cc = fa->ChildWeakPtr.lock();
cout << cc->say << endl;
return ;
}

std::weak_ptr的更多相关文章

  1. 智能指针std::weak_ptr

    std::weak_ptr 避免shared_ptr内存泄漏的利器.

  2. Item 20: 使用std::weak_ptr替换会造成指针悬挂的类std::shared_ptr指针

    本文翻译自modern effective C++,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 矛盾的是,我们很容易就能创造出一个和std::shared_ptr ...

  3. std::shared_ptr 和 std::weak_ptr的用法以及引用计数的循环引用问题

    在std::shared_ptr被引入之前,C++标准库中实现的用于管理资源的智能指针只有std::auto_ptr一个而已.std::auto_ptr的作用非常有限,因为它存在被管理资源的所有权转移 ...

  4. 智能指针weak_ptr解决循环依赖问题

    #include <iostream> #include <memory> class Woman; class Man{ private: std::weak_ptr< ...

  5. std::shared_ptr

    在std::shared_ptr被引入之前,C++标准库中实现的用于管理资源的智能指针只有std::auto_ptr一个而已.std::auto_ptr的作用非常有限,因为它存在被管理资源的所有权转移 ...

  6. C++ Primer : 第十二章 : 动态内存之unique_ptr和weak_ptr

    unique_ptr 一个unique_ptr拥有它所管理的对象,与shared_ptr不同,unique_ptr指向的对象只能有一个用户.当unique_ptr被销毁后,它所指向的对象也被销毁. 定 ...

  7. Qt 智能指针学习(7种QT智能指针和4种std智能指针)

    从内存泄露开始? 很简单的入门程序,应该比较熟悉吧 ^_^ #include <QApplication> #include <QLabel> int main(int arg ...

  8. C++ 11 创建和使用共享 weak_ptr

    1.为什么需要weak_ptr? 在正式介绍weak_ptr之前,我们先来回忆一下shared_ptr的一些知识.我们知道shared_ptr是采用引用计数的智能指针,多个shared_ptr实例可以 ...

  9. c++——智能指针学习(shared_ptr和weak_ptr)

    先看一个例子:Stark和Targaryen家族你中有我,我中有你.我们设计以下类企图避免内存泄漏,使得析构函数都能调用到: #include<iostream> #include< ...

随机推荐

  1. maven环境变量设置

    maven环境变量设置 maven环境变量设置 wondows 一.下载 开源网址:http://maven.apache.org/ 下载网址:http://maven.apache.org/down ...

  2. 字符串之————图文讲解字符串排序(LSD、MSD)

    本篇文章围绕字符串排序的核心思想,通过图示例子和代码分析的方式讲解了两个经典的字符串排序方法,内容很详细,完整代码放在文章的最后. 一.键索引计数法 在一般排序中,都要用里面的元素不断比较,而字符串这 ...

  3. 如何从请求、传输、渲染3个方面提升Web前端性能

    什么是WEB前端呢?就是用户电脑的浏览器所做的一切事情.我们来看看用户访问网站,浏览器都做了哪些事情: 输入网址 –> 解析域名 -> 请求页面 -> 解析页面并发送页面中的资源请求 ...

  4. linux中必备常用支持库的安装(CentOS)

    在CentOS安装软件的时候,可能缺少一部分支持库,而报错.这里首先安装系统常用的支持库.那么在安装的时候就会减少很多的错误的出现 yum install -y gcc gdb strace gcc- ...

  5. .net 和 java 技术对应关系

    .net 和 java 技术对应关系 java 生态的优势早已人尽皆知,asp.net core 的生态也呈欣欣向荣之势.作为高级技术从业人,你不应该落下任何一门技术. 如果你正在将你的 java 知 ...

  6. linux mint安装ffmpge

    sudo add-apt-repository ppa:mc3man/trusty-media sudo apt-get update sudo apt-get install ffmpeg

  7. centos7 Apache开启URL重写组件并配置.htaccess实现伪静态

    第一.修改httpd.conf文件 A - 在etc/httpd/conf/目录下的httpd.conf 文件,找到: LoadModule rewrite_module modules/mod_re ...

  8. 【ADO.NET基础知识】SqlConnection、command、DataSet 、DataTable、dataAdapter

    1.使用Connection连接数据库的步骤: (1).添加命名空间 System.Data.SqlClient(注意:初学者经常会忘记) (2)定义连接字符串.连接SQL Server 数据库时: ...

  9. 正睿OI国庆DAY2:图论专题

    正睿OI国庆DAY2:图论专题 dfs/例题 判断无向图之间是否存在至少三条点不相交的简单路径 一个想法是最大流(后来说可以做,但是是多项式时间做法 旁边GavinZheng神仙在谈最小生成树 陈主力 ...

  10. One layer SoftMax Classifier, "Handwriting recognition"

        import lib needed¶ In [1]: from PIL import Image import numpy as np import matplotlib.pyplot as ...