定义于头文件 <memory>

template< class T > class weak_ptr;    (C++11 起) 

std::weak_ptr 是一种智能指针,它对被 std::shared_ptr 管理的对象存在非拥有性(「弱」)引用。在访问所引用的对象前必须先转换为 std::shared_ptr。

std::weak_ptr 用来表达临时所有权的概念:当某个对象只有存在时才需要被访问,而且随时可能被他人删除时,可以使用 std::weak_ptr 来跟踪该对象。需要获得临时所有权时,则将其转换为 std::shared_ptr,此时如果原来的 std::shared_ptr 被销毁,则该对象的生命期将被延长至这个临时的 std::shared_ptr 同样被销毁为止。

std::weak_ptr 的另一用法是打断 std::shared_ptr 所管理的对象组成的环状引用。若这种环被孤立(例如无指向环中的外部共享指针),则 shared_ptr 引用计数无法抵达零,而内存被泄露。能令环中的指针之一为弱指针以避免此情况。

创建管理被引用的对象的shared_ptr

std::weak_ptr<T>::lock
std::shared_ptr<T> lock() const noexcept;   (C++11 起) 

创建新的 std::shared_ptr 对象,它共享被管理对象的所有权。若无被管理对象,即 *this 为空,则返回亦为空的 shared_ptr

等效地返回 expired() ? shared_ptr<T>() : shared_ptr<T>(*this) ,原子地执行。

参数

(无)

返回值

若 std::weak_ptr::expired 返回 false 则为共享被占有对象所有权的 shared_ptr 。否则返回默认构造的 T 类型的 shared_ptr

注意

此函数和 std::shared_ptr 的构造函数可能获得 std::weak_ptr 所指向的被管理对象的临时所有权。区别是 std::shared_ptr 的构造函数在其 std::weak_ptr 为空时抛异常,而 std::weak_ptr<T>::lock() 构造空的 std::shared_ptr<T> 。

调用示例

#include <iostream>
#include <memory>void observe(std::weak_ptr<int> weak)
{if (auto observe = weak.lock()){std::cout << "\tobserve() able to lock weak_ptr<>, value=" << *observe << "\n";}else{std::cout << "\tobserve() unable to lock weak_ptr<>\n";}
}int main()
{std::weak_ptr<int> weak;std::cout << "weak_ptr<> not yet initialized\n";observe(weak);{auto shared = std::make_shared<int>(42);weak = shared;std::cout << "weak_ptr<> initialized with shared_ptr.\n";observe(weak);}std::cout << "shared_ptr<> has been destructed due to scope exit.\n";observe(weak);
}

输出

提供弱指针的基于拥有者顺序

std::weak_ptr<T>::owner_before
template< class Y >
bool owner_before( const weak_ptr<Y>& other) const noexcept;template< class Y >
bool owner_before( const std::shared_ptr<Y>& other) const noexcept; 

以实现定义的基于拥有者(与基于值相反)顺序,检查此 weak_ptr 是否先于 other 。二个智能指针仅若都占有同一对象或均为空才比较相等,即使由 get() 获得的指针不同(例如因为它们指向同一对象中的不同子对象)。

此顺序用于令共享和弱指针可用作关联容器中的关键,通常经由 std::owner_less 。

参数

other - 要比较的 std::shared_ptr 或 std::weak_ptr

返回值

若 *this 前于 other 则为 true ,否则为 false 。常见实现比较控制块的地址。

调用示例

#include <iostream>
#include <memory>struct Foo
{int n1;int n2;Foo(int a, int b) : n1(a), n2(b) {}
};
int main()
{auto p1 = std::make_shared<Foo>(1, 2);std::shared_ptr<int> p2(p1, &p1->n1);std::shared_ptr<int> p3(p1, &p1->n2);std::cout << std::boolalpha<< "p2 < p3 " << (p2 < p3) << '\n'<< "p3 < p2 " << (p3 < p2) << '\n'<< "p2.owner_before(p3) " << p2.owner_before(p3) << '\n'<< "p3.owner_before(p2) " << p3.owner_before(p2) << '\n';std::weak_ptr<int> w2(p2);std::weak_ptr<int> w3(p3);std::cout<< "w2.owner_before(w3) " << w2.owner_before(w3) << '\n'<< "w3.owner_before(w2) " << w3.owner_before(w2) << '\n';auto p5 = std::make_shared<int>(6);std::weak_ptr<int> w5(p5);std::cout<< "w2.owner_before(w5) " << w2.owner_before(w5) << '\n'<< "w5.owner_before(w2) " << w5.owner_before(w2) << '\n';
}

输出

为weak_ptr赋值

std::weak_ptr<T>::operator=

weak_ptr& operator=( const weak_ptr& r ) noexcept;

(1) (C++11 起)

template< class Y >
weak_ptr& operator=( const weak_ptr<Y>& r ) noexcept;

(2) (C++11 起)

template< class Y >
weak_ptr& operator=( const shared_ptr<Y>& r ) noexcept;

(3) (C++11 起)

weak_ptr& operator=( weak_ptr&& r ) noexcept;

(4) (C++14 起)

template< class Y >
weak_ptr& operator=( weak_ptr<Y>&& r ) noexcept;

(5) (C++14 起)

r 所管理者替换被管理对象。与 r 共享该对象。若 r 不管理对象,则 *this 亦不管理对象。

1-3) 等价于 std::weak_ptr<T>(r).swap(*this) 。

4,5) 等价于 std::weak_ptr<T>(std::move(r)).swap(*this) 。

参数

r - 与之共享对象的智能指针

返回值

*this

注意

实现应满足要求而不创建临时的 weak_ptr 对象。

c++ 智能指针 (std::weak_ptr)(三)相关推荐

  1. C++ 智能指针std::shared_ptr简单使用和理解

    参考:https://blog.csdn.net/u011068702/article/details/83692838 1  智能指针std::shared_ptr相关知识和如何使用 我们这里先说下 ...

  2. C++之智能指针std::shared_ptr简单使用和理解

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程 1  智能指针std::shared_ptr相关知识和如何使用 我们这里先说下智能指针std::sha ...

  3. 深入学习c++--智能指针(二) weak_ptr(打破shared_ptr循环引用)

    1. 几种智能指针 1. auto_ptr: c++11中推荐不使用他(放弃) 2. shared_ptr: 拥有共享对象所有权语义的智能指针 3. unique_ptr: 拥有独有对象所有权语义的智 ...

  4. c语言 ptr 用法,C++之智能指针std::shared_ptr简单使用和理解

    1  智能指针std::shared_ptr相关知识和如何使用 我们这里先说下智能指针std::shared_ptr,因为我看到我我们项目c++代码里面用得很多,我不是不会,所以记录学习下 先让ubu ...

  5. 【C++11新特性】 C++11智能指针之weak_ptr

    http://blog.csdn.net/xiejingfa/article/details/50772571 原创作品,转载请标明:http://blog.csdn.net/Xiejingfa/ar ...

  6. c++11新特性_【C++11新特性】 C++11智能指针之weak_ptr

    如题,我们今天要讲的是 C++11 引入的三种智能指针中的:weak_ptr. 在学习 weak_ptr 之前最好对 shared_ptr 有所了解.如果你还不知道 shared_ptr 是何物,可以 ...

  7. C++智能指针:weak_ptr实现详解

    文章目录 weak_ptr描述 声明 作用 原理实现 函数成员使用 总结 weak_ptr描述 声明 头文件:<memory> 模版类:template <class T> c ...

  8. C++笔记:智能指针 std::shared_ptr

    1. 语法 std::shared_ptr<类型> 变量名称{}: std::shared_ptr<int> ptrA{};std::shared_ptr<int> ...

  9. webRTC之智能指针std::unique_ptr::reset()使用(十四)

    一.std::unique_ptr::reset()使用 void reset (pointer p = pointer()) noexcept; 1.src/modules/audio_device ...

  10. [C++11]弱引用智能指针weak_ptr初始化和相关的操作函数

    弱引用智能指针 std::weak_ptr 可以看做是 shared_ptr 的助手,它不管理 shared_ptr 内部的指针.std::weak_ptr 没有重载操作符 * 和 ->,因为它 ...

最新文章

  1. HashMap为何从头插入改为尾插入
  2. 小程序商城制作,轻松打造自己的会员系统
  3. python进阶书籍的推荐 知乎-知乎看了很多推荐,最终选了这本Python入门
  4. 【Linux】4_基本权限ACL的命令(setacl和getacl)
  5. 3.环境搭建-Hadoop(CDH)集群搭建
  6. PHP实现内部函数explode
  7. 山工kw什么意思_【山工装载机故障灯KW什么意思】专区-山工-装载机-铁甲网
  8. 数据结构基础(8) --单链表的设计与实现(1)之基本操作
  9. 深入玩转K8S之智能化的业务弹性伸缩和滚动更新操作
  10. ORACLE TRUNC()函数
  11. 【kindeditor】kindeditor的使用
  12. 必须了解的一些IT知识点
  13. Kindle PaperWhite 3 5.8.10越狱成功!
  14. linux网络串口工具下载,串口调试工具手机版下载
  15. SQL server2019导入Access 2016数据失败解决方法
  16. 人脸识别翼闸使用规范_介绍市场上最常用的通道翼门和人脸识别的使用
  17. 基于python/scipy学习概率统计(2):伯努利分布(Bernoulli Distribution)
  18. 论文(四):DTFD-MIL: Double-Tier Feature Distillation Multiple Instance Learning for Histopathology Whole
  19. oracle期初余额录入,用金蝶kis录入数量初始数据的方法
  20. 区块链到底能做什么?这506个备案项目告诉你

热门文章

  1. 台式计算机能不能安装蓝牙驱动,台式电脑蓝牙驱动安装失败怎么办?台式电脑蓝牙驱动安装失败解决办法...
  2. 防止backspace键后退网页
  3. 中国10大经典徒步线路(资深徒步专家@行摄匆匆推荐)
  4. 慧荣SM2262EN跑RDT教程
  5. 考研常考知识点(复试)
  6. 导弹防御系统(LIS)
  7. npm init 自动创建 package.json 文件
  8. TensorFlow的Dataset的padded_batch使用
  9. [数据库笔记] SQL50题11 - 30
  10. 地理信息系统实习作业——利用ArcGis计算武汉市分区土地利用类型面积