转自 https://blog.csdn.net/ajioy/article/details/7377099
weak_ptr是为配合shared_ptr而引入的一种智能指针,它更像是shared_ptr的一个助手,而不是智能指针,因为它不具有普通指针的行为,没有重载operator*和operator->,它的最大作用在于协助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<T> lock() const;
-
- void reset();
- void swap(weak_ptr<T> &b);
- };
weak_ptr是一个“弱”指针,但它能够完成一些特殊的工作,足以证明它的存在价值。
weak_ptr被设计为与shared_ptr共同工作,可以从一个shared_ptr或者另一个weak_ptr对象构造,获得资源的观测权。但weak_ptr没有共享资源,它的构造不会引起指针引用计数的增加。同样,在weak_ptr析构时也不会导致引用计数的减少,它只是一个静静地观察者。
使用weak_ptr的成员函数use_count()可以观测资源的引用计数,另一个成员函数expired()的功能等价于use_count() == 0,但更快,表示观测的资源(也就是shared_ptr管理的资源)已经不复存在了。
weak_ptr 没有重载operator*和->,这是特意的,因为它不共享指针,不能操作资源,这是它弱的原因。但它可以使用一个非常重要的成员函数lock()从被观测的shared_ptr获得一个可用的shared_ptr对象,从而操作资源。当expired() == true的时候,lock()函数将返回一个存储空指针的shared_ptr。
使用示例:
- int main(){
- shared_ptr<int> sp(new int(10));
- assert(sp.use_count() == 1);
- //create a weak_ptr from shared_ptr
- weak_ptr<int> wp(sp);
- //not increase the use count
- assert(sp.use_count() == 1);
- //judge wp is invalid
- //expired() is equivalent with use_count() == 0
- if(!wp.expired()){
- shared_ptr<int> sp2 = wp.lock();//get a shared_ptr
- *sp2 = 100;
- assert(wp.use_count() == 2);
- cout << *sp2 << endl;
- }//out of scope,sp2 destruct automatically,use_count()--;
- assert(wp.use_count() == 1);
- sp.reset();//shared_ptr is invalid
- assert(wp.expired());
- assert(!wp.lock());
- }
获得this的shared_ptr
weak_ptr的一个重要用途是获得this指针的shared_ptr,使对象自己能够生产shared_ptr管理自己:对象使用weak_ptr观测this指,这并不影响引用计数,在需要的时候就调用lock()函数,返回一个符合要求的shared_ptr使外界使用。
这个解决方案被实现为一个惯用法,在头文件<booost/enable_shared_from_this.hpp>定义了一个助手类enable_shared_from_this<T>,其声明如下:
- template<class T>
- class enable_shared_from_this
- {
- public:
- shared_ptr<T> shared_from_this();
- }
使用的时候只需要让想被shared_ptr管理的类从它继承即可,成员函数shared_from_this()会返回this的shared_ptr
使用示例:
- #include <iostream>
- #include <boost/smart_ptr.hpp>
- #include <boost/enable_shared_from_this.hpp>
- #include <boost/make_shared.hpp>
- using namespace boost;
- using namespace std;
- class self_shared:
- public enable_shared_from_this<self_shared>{
- public:
- self_shared(int n):x(n){}
- int x;
- void print(){
- cout << "self_shared:" << x << endl;
- }
- };
- int main(){
- shared_ptr<self_shared> sp =
- make_shared<self_shared>(315);
- sp->print();
- shared_ptr<self_shared> p = sp->shared_from_this();
- p->x = 100;
- p->print();
-
- }
运行结果:
self_shared:315
self_shared:100
需要注意的是千万不能从一个普通对象(非shared_ptr)使用shared_from_this ()获取shared_ptr,如
self_shared ss;
shaerd_ptr<self_shared> p = ss.shared_from_this();//error
这样虽然语法上能通过,编译也无问题,但在运行时会导致shared_ptr析构时企图删除一个栈上分配的对象,发生未定义行为。
关于intrusive_ptr
intrusive_ptr是一个侵入式的引用计数型指针,它可以用于以下情形:
对内存占用的要求非常严格,要求必须与原始指针一样;
现存代码已经有了引用计数机制管理的对象。
boost库不推荐intrusive_ptr,因为shared_ptr已经非常强大且灵活,工作得足够好,可以满足绝大部分(99.99%)的需要。
如果有兴趣,可以自行翻阅boost文档。