`

weak_ptr

阅读更多
weak_ptr是为配合shared_ptr而引入的一种智能指针来协助shared_ptr工作,它可以从一个shared_ptr或另一个weak_ptr对象构造,它的构造和析构不会引起引用记数的增加或减少。没有重载*和->但可以使用lock获得一个可用的shared_ptr对象
template<class T> class weak_ptr
{
public:
    weak_ptr();

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

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

    long use_count()const;
    //判断是否过期
    bool expired()const;
    //得到一个空的或者被协助的shared_ptr
    shared_ptr<T> lock()const;

    void reset();
    void swap(weak_ptr<T> & b);
};



#include<iostream>
#include<boost/weak_ptr.hpp>
#include<boost/shared_ptr.hpp>
using namespace std;
using namespace boost;

int main()
{
    shared_ptr<int> p(new int(10));
    weak_ptr<int> w(p);
    while(!w.expired()){
        cout << w.use_count() << endl;
        shared_ptr<int> t = w.lock();
        cout << *t << endl;
        cout << w.use_count() << endl;
        if(w.use_count()==2){
            break;
        }
    }
    w.reset();
    cout << w.expired() << endl;
}

1
10
2
1


weak_ptr的一个重要用途是通过lock获得this指针的shared_ptr,使对象自己能够生产shared_ptr来管理自己,但助手类enable_shared_from_this的shared_from_this会返回this的shared_ptr,只需要让想被shared_ptr管理的类从它继承即可

#include<boost/enable_shared_from_this.hpp>
#include<boost/make_shared.hpp>
#include<iostream>
using namespace boost;
using namespace std;

class testWeak:public enable_shared_from_this<testWeak>{//
public:
    int i;
    testWeak(int ii):i(ii){}
    void print(){
        cout << i << endl;
    }
};

int main()
{
    shared_ptr<testWeak> sp = make_shared<testWeak>(100);
    shared_ptr<testWeak> tw = sp->shared_from_this();
    tw->print();
}

100
分享到:
评论

相关推荐

    C++ 智能指针(shared_ptr/weak_ptr)源码

    C++ 智能指针(shared_ptr/weak_ptr)源码 源码位置:gcc-6.1.0\gcc-6.1.0\libstdc++-v3\include\tr1 这里只单列shared_ptr.h文件用于分析

    C++11智能指针之weak_ptr详解

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

    C++ unique_ptr weak_ptr shared_ptr auto_ptr智能指针.doc

    四种智能指针的使用、机制和缺陷分析

    C++11新特性之智能指针(shared_ptr/unique_ptr/weak_ptr)

    主要介绍了C++11新特性之智能指针,包括shared_ptr, unique_ptr和weak_ptr的基本使用,感兴趣的小伙伴们可以参考一下

    浅析Boost智能指针:scoped_ptr shared_ptr weak_ptr

    scoped_ptrboost::scoped_ptr和std::auto_ptr非常类似,是一个简单的智能指针,它能够保证在离开作用域后对象被自动释放。下列代码演示了该指针的基本应用: 代码如下:#include &lt;string&gt;#include &lt;iostream&gt;#...

    weak-ptr 和 shared-ptr 源码

    关于C++智能指针 weak_ptr (弱引用 智能指针) 和 shared_ptr (强引用 智能指针)的源文件。 资源分不能设为0了。。

    C++11 智能指针之shared_ptr代码详解

    在新标准中,主要提供了shared_ptr、unique_ptr、weak_ptr三种不同类型的智能指针。 接下来的几篇文章,我们就来总结一下这些智能指针的使用。 今天,我们先来看看shared_ptr智能指针。 shared_ptr 智能指针 shared_...

    C++11智能指针中的 unique_ptr实例详解

    在前面一篇文章中,我们了解了 C++11 中引入的智能指针之一 shared_ptr 和 weak_ptr ,今天,我们来介绍一下另一种智能指针 unique_ptr 。 往期文章参考: 【C++11新特性】 C++11 智能指针之shared_ptr 【C++11新...

    84、智能指针的原理、常用的智能指针及实现.pdf

    如果⼀块内存被shared_ptr和weak_ptr同时引⽤,当所有shared_ptr析构了之 后,不管还 有没有weak_ptr引⽤该内存,内存也会被释放。所以weak_ptr不保证它指向的内存⼀定是 有效的,在使⽤之前使⽤函数lock()检查weak_...

    程序员为什么还要刷题-rcu_ptr:用于在线程之间交换数据的特殊智能指针

    rcu_ptr通过包装std::shared_ptr实现读取复制更新机制(在某种程度上,它与std::weak_ptr有一些相似之处)。 atomic_shared_ptr rcu_ptr依赖于std::shared_ptr的自由函数重载。 使用 会很好,但目前仍处于实验阶段。...

    【C++ 深入浅出】智能指针shared_ptr、unique_ptr、weak_ptr详解

    C++ 11中最常用的智能指针类型为shared_ptr,它采用引用计数的方法,记录当前内存资源被多少个智能指针引用。该引用计数的内存在堆上分配。当新增一个时引用计数加1,当过期时引用计数减一。只有引用计数为0时,智能...

    C++智能指针shared-ptr讲解与使用.pdf

    为了在结构复杂的情境中执⾏上述⼯作,标准库提供了weak_ptr、bad_weak_ptr和 enable_shared_from_this等辅助类。 2. Class unique_ptr实现独占式拥有(exclusive ownership)或严格拥有(strict ownership)概念,...

    C++智能指针详解(1).pdf

    标准库还定义了⼀种名为weak_ptr的伴随类,它是⼀种弱引⽤,指向shared_ptr所管理的对象,这三种智能指针都定义在memory头⽂件 中。 2、auto_ptr (不要使⽤的指针) (1)内部⼤概实现:做成⼀个auto_ptr类,包含...

    C++智能指针循环引用问题分析.pdf

    C++智能指针循环引⽤问题分析 C++11中引⼊了三种智能指针,分别是shared_ptr、weak_ptr和unique_ptr 智能指针的作⽤ 智能指针可以帮助我们管理动态分配的堆内存,减少内存泄漏的可能性 ⼿动管理堆内存有引起内存泄漏...

    智能指针shared-ptr的用法.pdf

    C++11提供了三种智能指针:std::shared_ptr, std::unique_ptr, std::weak_ptr,使⽤时需添加头⽂件。 shared_ptr使⽤引⽤计数,每⼀个shared_ptr的拷贝都指向相同的内存。每使⽤他⼀次,内部的引⽤计数加1,每析构...

    C++智能指针.pdf

    C++ 标准库有四种智能指针:auto_ptr,unique_ptr,shared_ptr,weak_ptr(auto_ptr 是 C++98 标准的,其余都是 C++11 标准推出的,auto_ptr 现在已经不再使⽤了),C++11 这三种智能指针都是类模板。 ⼆ ⼆. ...

    C++智能指针的原理和实现.pdf

    ⼆、智能指针类型 ⼆、智能指针类型 智能指针在C++11版本之后提供,包含在头⽂件中,标准命名std空间下,有auto_ptr、shared_ptr、weak_ptr、unique_ptr四 种,其中auto_ptr已被弃⽤。 :拥有严格对象所有权语义的...

Global site tag (gtag.js) - Google Analytics