shared_from_this()是enable_shared_from_this<T>的成员 函数,返回shared_ptr<T>。首先需要注意的是,这个函数仅在shared_ptr<T>的构造函数被调用之后才能使 用。原因是enable_shared_from_this::weak_ptr并不在构造函数中设置,而是在shared_ptr<T>的 构造函数中设置。
如下代码是错误的:
如下代码也是错误的:
如下代码是正确的:
结论是,不要在构造函数中使用shared_from_this;其次,如果要使用shared_ptr,则应该 在所有地方均使用,不能使用D d这种方式,也决不要传递裸指针。
另一个值得注意的地方是在类的继承树中不能有2个或更多个enable_shared_from_this<T>。例如如下代码是错误的:
class B:public boost::enable_shared_from_this<B>,public A,
则仅设置enable_shared_from_this<B>的weak_ptr。很明显都是错误的。
那么enable_shared_from_this以及shared_ptr为何要如此实现呢?又为什么会有如此怪异的结果呢?
首先考察shared_ptr的构造函数:
那么正确的解法是怎样的呢?
关于为什么enable_shared_from_this是这样实现的,可以参看作者原文:
Every enable_shared_from_this base contains a weak_ptr, The shared_ptr constructor looks up the enable_shared_from_this base and initializes its weak_ptr accordingly. This doesn't work when there are
two or more enable_shared_from_this bases, though.
I could put the weak_ptr in a virtual polymorphic base. This would force polymorphism on all clients of enable_shared_from_this... probably acceptable. It will also force a dynamic_pointer_cast in every
shared_from_this, and this may be harder to swallow, particularly in cases where RTTI is off. So I'm not sure.
If you do want the above behavior, it's easy to duplicate, as I already responded in my first post on the topic. Just make FooB return dynamic_pointer_cast<B>( FooA() ) and remove the enable_shared_from_this<B>
base (A needs to be made polymorphic, of course).
注意为了让dynamic_pointer_cast能工作,A必须具有虚函数,那么最简单的做法当然是令其析构函 数为虚函数(通常一个class如果希望被继承,析构函数就应该为虚函数)。
如下代码是错误的:
- class D:public boost::enable_shared_from_this<D>
- {
- public:
- D()
- {
- boost::shared_ptr<D> p=shared_from_this();
- }
- };
如下代码也是错误的:
- class D:public boost::enable_shared_from_this<D>
- {
- public:
- void func()
- {
- boost::shared_ptr<D> p=shared_from_this();
- }
- };
- void main()
- {
- D d;
- d.func();
- }
如下代码是正确的:
- void main()
- {
- boost::shared_ptr<D> d(new D);
- d->func();
- }
结论是,不要在构造函数中使用shared_from_this;其次,如果要使用shared_ptr,则应该 在所有地方均使用,不能使用D d这种方式,也决不要传递裸指针。
另一个值得注意的地方是在类的继承树中不能有2个或更多个enable_shared_from_this<T>。例如如下代码是错误的:
- class A:public boost::enable_shared_from_this<A>
- {
- public:
- A():a(1){}
- virtual ~A(){}
- boost::shared_ptr<A> get_ptra(){return shared_from_this();}
- int a;
- };
- class B:public A,public boost::enable_shared_from_this<B>
- {
- public:
- B():b(2){}
- boost::shared_ptr<B> get_ptrb()
- {
- return boost::enable_shared_from_this<B>::shared_from_this();
- }
- int b;
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- {
- boost::shared_ptr<B> x(new B);
- boost::shared_ptr<A> a1 = x->get_ptra();
- boost::shared_ptr<B> b1 = x->get_ptrb();
- }
- return 0;
- }
class B:public boost::enable_shared_from_this<B>,public A,
则仅设置enable_shared_from_this<B>的weak_ptr。很明显都是错误的。
那么enable_shared_from_this以及shared_ptr为何要如此实现呢?又为什么会有如此怪异的结果呢?
首先考察shared_ptr的构造函数:
- template<class Y>
- explicit shared_ptr( Y * p ): px( p ), pn( p ) // Y must be complete
- {
- boost::detail::sp_enable_shared_from_this( pn, p, p );
- }
- template<class T, class Y> void sp_enable_shared_from_this( shared_count const & pn, boost::enable_shared_from_this<T> const * pe, Y const * px )
- {
- if(pe != 0) pe->_internal_weak_this._internal_assign(const_cast<Y*>(px), pn);
- }
那么正确的解法是怎样的呢?
- class B:public A
- {
- public:
- B():b(2){}
- boost::shared_ptr<B> get_ptrb()
- {
- return boost::dynamic_pointer_cast<B>(shared_from_this());
- }
- int b;
- };
关于为什么enable_shared_from_this是这样实现的,可以参看作者原文:
Every enable_shared_from_this base contains a weak_ptr, The shared_ptr constructor looks up the enable_shared_from_this base and initializes its weak_ptr accordingly. This doesn't work when there are
two or more enable_shared_from_this bases, though.
I could put the weak_ptr in a virtual polymorphic base. This would force polymorphism on all clients of enable_shared_from_this... probably acceptable. It will also force a dynamic_pointer_cast in every
shared_from_this, and this may be harder to swallow, particularly in cases where RTTI is off. So I'm not sure.
If you do want the above behavior, it's easy to duplicate, as I already responded in my first post on the topic. Just make FooB return dynamic_pointer_cast<B>( FooA() ) and remove the enable_shared_from_this<B>
base (A needs to be made polymorphic, of course).
注意为了让dynamic_pointer_cast能工作,A必须具有虚函数,那么最简单的做法当然是令其析构函 数为虚函数(通常一个class如果希望被继承,析构函数就应该为虚函数)。