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. 解决chrome浏览器崩溃,再次安装不上问题

    上网重新下载了个安装包,发现安装包都打不来 很绝望,查了很多资料 很多人说要删除注册表的东西 但是打开注册表,发现一堆google的东西,手动删根本不现实 在绝望中看到了解决方案:google Upd ...

  2. 字符串之————三向字符串快速排序(Quick3string)

    上一篇介绍了字符串的两种经典排序方法(LSD MSD): https://www.cnblogs.com/Unicron/p/11531111.html 下面我们来介绍一种通用的字符串排序方法——三向 ...

  3. 25 (OC)* iOS网络HTTP、TCP、UDP、Socket 知识总结

    应用层:1.用户接口.应用程序:2.Application典型设备:网关:3.典型协议.标准和应用:TELNET.FTP.HTTP 表示层:1.数据表示.压缩和加密presentation2.典型设备 ...

  4. 学习方法分享:为何一年半就能拿到大厂 offer

    毕竟是聊聊曾经,放一张大学课堂上灵光一现,手写的一个我曾经一直使用的网名 前言 原文地址:Nealyang/personalBlog 讲真,的确是运气,才有机会进大厂.也没想到,那篇一年半工作经验试水 ...

  5. cent OS 7 忘记 root 密码

    1. 在如下图, 选择系统的界面 按 e 2. 移动光标到文件底部, 修改如下两个地方(初始化 shell文件 并设置可读写), Ctrl x 退出并启动 shell 3. 如下界面 输入命令 mou ...

  6. Salem and Sticks-萨鲁曼的棍子 CodeForce#1105A 暴力

    题目链接:Salem and Sticks 题目原文 Salem gave you 

  7. Guava的常用方法示例

    Guava Maven Dependency <dependency> <groupId>com.google.guava</groupId> <artifa ...

  8. 分布式session共享机制分析

    使用配置: 1.在pom文件中引入spring-session的jar包 <!--springsession--><dependency><groupId>org. ...

  9. Oracle clob列union的方法(ORA-00932)

    今天在做“站内搜索”数据抽取时,为了能将多个相似的数据库表数据合并,使用了SQL中union关键字,期望将多个单独的SQL查询结果合并到一起.每个单独的SQL都能成功执行,在union合并的过程中遇到 ...

  10. Spring 梳理-@Controller

    @Controller是一个构造性注解(stereotype),它基于@Component 在自动扫描中,组件扫描器会自动将@Controller申明的类注册为Spring应用上下文的一个bean 可 ...