Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2003-07-18 12:23:24


Jason Winnebeck wrote:
> Can you multiply inherit from objects that inherit from
> enable_shared_from_this? It seems not. My program compiled on
> MSVC.NET "crashes" due to shared_from_this throwing
> boost::bad_weak_ptr.

[...]

> class Base1 : public SharedThis<Base1> {

[...]

> class Base2 : public SharedThis<Base2> {

[...]

> class Child : public Base1, public Base2 {

No, you can't do that. Multiple enable_shared_from_this<> bases are not
supported, there is no way to enumerate them.

The typical enable_shared_from_this use would look like:

class Base1; // abstract
class Base2; // abstract

class Child: public Base1, public Base2, public
enable_shared_from_this<Child>;

Multiple inheritance from concrete classes is just problematic, in more ways
that just enable_shared_from_this not working. :-)

BTW, if you have a static creation function, and it seems that you do:

   static sptr create() {
     return sptr( new Base1() );
   }

consider whether you only need a "shared_ptr from this" at construction
time, and if so, simply perform the necessary registration in create():

   static sptr create() {
     sptr this_( new Base1() );
     register(this_);
     return this_;
   }

In general, enable_shared_from_this is to be avoided. You only need it in
some rare cases.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net