Use of enable_shared_from_this with multiple inheritance

20,587

Indeed you are doing it wrong. If you have simple inheritance, just inherit from enable_shared_from this in the base class, and derived class get it for free. (of course you'll need to downcast the result)

If you have multiple inheritance (like it seems), you must use the trick described here and also here :

/* Trick to allow multiple inheritance of objects
 * inheriting shared_from_this.
 * cf. https://stackoverflow.com/a/12793989/587407
 */

/* First a common base class
 * of course, one should always virtually inherit from it.
 */
class MultipleInheritableEnableSharedFromThis: public std::enable_shared_from_this<MultipleInheritableEnableSharedFromThis>
{
public:
  virtual ~MultipleInheritableEnableSharedFromThis()
  {}
};

template <class T>
class inheritable_enable_shared_from_this : virtual public MultipleInheritableEnableSharedFromThis
{
public:
  std::shared_ptr<T> shared_from_this() {
    return std::dynamic_pointer_cast<T>(MultipleInheritableEnableSharedFromThis::shared_from_this());
  }
  /* Utility method to easily downcast.
   * Useful when a child doesn't inherit directly from enable_shared_from_this
   * but wants to use the feature.
   */
  template <class Down>
  std::shared_ptr<Down> downcasted_shared_from_this() {
    return std::dynamic_pointer_cast<Down>(MultipleInheritableEnableSharedFromThis::shared_from_this());
  }
};

Then your code becomes :

class A: public inheritable_enable_shared_from_this<A>
{
public:
    void foo1()
    {
        auto ptr = shared_from_this(); 
    }
};

class B: public inheritable_enable_shared_from_this<B>
{
public:
    void foo2()
    {
        auto ptr = shared_from_this(); 
    }
};

class C: public inheritable_enable_shared_from_this<C>
{
public:
    void foo3()
    {
        auto ptr = shared_from_this(); 
    }
};

class D: public A, public B, public C
{
public:
    void foo()
    {
        auto ptr = A::downcasted_shared_from_this<D>(); 
    }
};
Share:
20,587

Related videos on Youtube

Aarkan
Author by

Aarkan

Updated on September 01, 2020

Comments

  • Aarkan
    Aarkan over 3 years

    BI am using enable_shared_from_this in my code, and I am not sure if its usage is correct. This is the code:

    class A: public std::enable_shared_from_this<A>
    {
    public:
        void foo1()
        {
            auto ptr = shared_from_this(); 
        }
    };
    
    class B:public std::enable_shared_from_this<B>
    {
    public:
        void foo2()
        {
            auto ptr = shared_from_this(); 
        }
    };
    
    class C:public std::enable_shared_from_this<C>
    {
    public:
        void foo3()
        {
            auto ptr = shared_from_this(); 
        }
    };
    
    class D: public A, public B, public C
    {
    public:
        void foo()
        {
            auto ptr = A::shared_from_this(); 
        }
    };
    

    Are these usage of make_shared_from_this() correct, assuming that they are always being called through shared_ptr of D?

    • aschepler
      aschepler about 11 years
      I don't think foo2 or foo3 would compile...
    • Stephane Rolland
      Stephane Rolland about 11 years
      yep that doesn't make sense, only class A inherits enable_shared_from_this<>
    • indeterminately sequenced
      indeterminately sequenced about 11 years
      I think you should take a look at what enable_shared_from_this does. See the answer to this question
    • Andriy Tylychko
      Andriy Tylychko about 11 years
    • Aarkan
      Aarkan about 11 years
      I apologize for the syntactical mistakes. Updated the code.
    • Stephane Rolland
      Stephane Rolland about 11 years
      yep that's better :-)
  • Andrew
    Andrew almost 7 years
    Note: derived classes will only be able to use the inherited enable_shared_from_this if it is public, i.e.: class BaseClass: public enable_shared_from_this<BaseClass>.
  • Ehtesham Hasan
    Ehtesham Hasan about 6 years
    @Offirmo Is there any possible solution in case I am unable to modify (inherit from inheritable_enable_shared_from_this) the base classes (A, B, C)?