shared_ptr
来源:互联网 发布:淘宝网黑盘羊沉香好吗 编辑:程序博客网 时间:2024/05/01 18:59
using namespace std;class Test{public: Test() { cout << "Test" << endl;} ~Test() { cout << "~Test" << endl;}private: Test(const Test &); void operator=(const Test &);};int main(int argc, char const *argv[]){ shared_ptr<Test> ptr(new Test); shared_ptr<Test> ptr2(ptr); cout << ptr.use_count() << endl; return 0;}
#include <memory>#include <iostream>#include <string>using namespace std;class Parent;class Child;typedef shared_ptr<Parent> ParentPtr;typedef shared_ptr<Child> ChildPtr;class Parent{public: ~Parent() { cout << "~Parent" << endl; } ChildPtr child_;};class Child{public: ~Child() { cout << "~Child" << endl;} ParentPtr parent_;};int main(int argc, char const *argv[]){ { ParentPtr parent(new Parent); ChildPtr child(new Child); parent->child_ = child; child->parent_ = parent; cout << "parent count: " << parent.use_count() << endl; cout << "child count: " << child.use_count() << endl; } return 0;}
#include <memory>#include <iostream>#include <string>using namespace std;class Parent;class Child;typedef shared_ptr<Parent> ParentPtr;typedef shared_ptr<Child> ChildPtr;class Parent{public: ~Parent() { cout << "~Parent" << endl; } ChildPtr child_;};class Child{public: Child(const ParentPtr &parent) :parent_(parent) { } ~Child() { cout << "~Child" << endl;} //ParentPtr parent_; std::weak_ptr<Parent> parent_;};int main(int argc, char const *argv[]){ { ParentPtr parent(new Parent); ChildPtr child(new Child(parent)); parent->child_ = child; cout << "parent count: " << parent.use_count() << endl; cout << "child count: " << child.use_count() << endl; } return 0;}使用情景:当类对象被 shared_ptr 管理时,需要在类自己定义的函数里把当前类对象作为参数传给其他函数时,这时需要传递一个 shared_ptr ,否则就不能保持 shared_ptr 管理这个类对象的语义(因为有一个 raw pointer 指向这个类对象,而 shared_ptr 对类对象的这个引用没有计数,很有可能 shared_ptr 已经把类对象资源释放了,而那个调用函数还在使用类对象——显然,这肯定会产生错误)。
很好奇这个模板类的实现。
先看看怎么使用:
对一个类 A ,当我们希望使用 shared_ptr 来管理其类对象时,而且需要在自己定义的函数里把类对象 shared_ptr (为什么不用普通指针,当我们使用智能指针管理资源时,必须统一使用智能指针,而不能在某些地方使用智能指针某些地方使用 raw pointer ,否则不能保持智能指针的语义,从而产生各种错误)传给其他函数时,可以让类 A 从 enable_shared_from_this 继承:
class A : public boost::enable_shared_from_this<A> {
};
然后在类 A 中需要传递类对象本身 shared_ptr 的地方使用 shared_from_this 函数来获得指向自身的 shared_ptr 。
一个非常有代表性的例子:
http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/tutorial/tutdaytime3/src.html
另《Beyond the C++ Standard Library》 shared_ptr 节也有很简单明了的例子。
实现原理:
首先要考虑的是:在类对象本身当中不能存储类对象本身的 shared_ptr ,否则类对象 shared_ptr 永远也不会为0了,从而这些资源永远不会释放,除非程序结束。
其次:类对象肯定是外部函数通过某种机制分配的,而且一经分配立即交给 shared_ptr 管理(再次强调一遍:给 shared_ptr 管理的资源必须在分配时交给 shared_ptr ),而且以后凡是需要共享使用类对象的地方必须使用这个 shared_ptr 当作右值来构造产生或者拷贝产生另一个 shared_ptr 从而达到共享使用的目的。
有了以上两点的限制,要实现我们的目标(即在类对象内部使用类对象的 shared_ptr )有以下两种方案:
1、类对象的外部 shared_ptr 作为函数参数传给类的需要引用类对象自身的函数——显然,这种方法很丑陋,而且并不是所有的情况都可行(如在外部 shared_ptr 不可见的作用域中就不行);
2、类对象自身存储某种信息,在需要自身 shared_ptr 时来产生一个临时的 shared_ptr 。
显然,第2种方法更优雅(对于用户来说),关键是信息怎么存储?
对了, weak_ptr !
实际上, boost 中就是这样实现的。
但现在的问题是:何时初始化这个 weak_ptr ?因为类对象生成时还没有生成相应的用来管理这个对象的 shared_ptr 。
boost 1.39.0 中是这样实现的:
首先生成类 A :会依次调用 enable_shared_from_this 的构造函数(定义为 protected ),以及类 A 的构造函数。在调用 enable_shared_from_this 的构造函数时,会初始化定义在 enable_shared_from_this 中的 weak_ptr (调用其默认构造函数),这时这个 weak_ptr 是无效的(或者说不指向任何对象)。
接着:外部程序会把指向类 A 对象的指针作为初始化参数来初始化一个 shared_ptr 。
现在来看看 shared_ptr 是如何初始化的, shared_ptr 定义了如下构造函数:
template<class Y>
explicit shared_ptr( Y * p ): px( p ), pn( p )
{
boost::detail::sp_enable_shared_from_this( this, p, p );
}
里面调用了 boost::detail::sp_enable_shared_from_this :
template< class X, class Y, class T >
inline void sp_enable_shared_from_this( boost::shared_ptr<X> const * ppx,
Y const * py, boost::enable_shared_from_this< T > const * pe )
{
if( pe != 0 )
{
pe->_internal_accept_owner( ppx, const_cast< Y* >( py ) );
}
}
里面又调用了 enable_shared_from_this 的 _internal_accept_owner :
template<class X, class Y> void _internal_accept_owner( shared_ptr<X> const * ppx, Y * py ) const
{
if( weak_this_.expired() )
{
weak_this_ = shared_ptr<T>( *ppx, py );
}
}
而在这里对 enable_shared_from_this 的成员 weak_ptr 进行拷贝赋值,使得整个 weak_ptr 作为类对象 shared_ptr 的一个观察者。
这时,当类对象本身需要自身的 shared_ptr 时,就可以从这个 weak_ptr 来生成一个了。
原来如此。
0 0
- shared_ptr
- shared_ptr
- shared_ptr
- shared_ptr
- shared_ptr
- shared_ptr
- shared_ptr
- shared_ptr
- shared_ptr
- shared_ptr
- shared_ptr
- shared_ptr
- shared_ptr()
- shared_ptr
- shared_ptr
- shared_ptr
- shared_ptr
- shared_ptr
- 1003. Emergency (25)
- UVA - 227 Puzzle
- GitHub Hacking
- Swift - 元组的用法
- OFBiz安装注意事项:不要启动Tomcat
- shared_ptr
- 【转自网上,分享更多人看到】创建符号链接 “asm”: 不支持的操作
- Android decode
- JAVA+jni 包名或方法名中含有下划线(_)的解决方法
- 删除分区表students子分区sex_male中年龄大于30岁的记录
- cocos2dx2.2 mac下创建工程命令
- hdu 5493 Queue(线段树)
- Do use segment tree 树链剖分,按顺序剖分
- 欢迎使用CSDN-markdown编辑器