Multiple Inheritance and enable_shared_from_this

http://www.codeproject.com/Articles/286304/Solution-for-multiple-enable_shared_from_this-in-i

http://boost.2283326.n4.nabble.com/smart-ptr-enable-shared-from-this-and-multiple-inheritance-solution-td3058364.html

http://stackoverflow.com/questions/5232712/questions-regarding-shared-from-this

1)shared_from_this enables you to guarantee that if you have one shared_ptr instance to your object then you can construct another without copying the first, and that these instances will share the reference count. You could achieve this by storing a weak_ptr as a class member, and setting that value when you first allocate a shared_ptr to your object.

2) Calling shared_from_this() requires that there is at least one shared_ptr instance already pointing to your object. If you use it on an automatic object without a shared_ptr instance with a custom deleter then you will get bad stuff happening.

3) If you derive from your class then the enable_shared_from_this functionality will give you a shared_ptr to the base class (the one that derived from enable_shared_from_this). You could then use static_pointer_cast or dynamic_pointer_cast to cast the result of shared_from_this() to a pointer to the derived class.

posted on 2013-02-03 14:56  androidme  阅读(373)  评论(0编辑  收藏  举报

导航