【问题标题】:Use of enable_shared_from_this with multiple inheritance在多重继承中使用 enable_shared_from_this
【发布时间】:2013-04-11 13:23:31
【问题描述】:

BI 在我的代码中使用enable_shared_from_this,我不确定它的用法是否正确。这是代码:

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(); 
    }
};

make_shared_from_this() 的这些用法是否正确,假设它们总是通过 D 的 shared_ptr 调用?

【问题讨论】:

  • 我认为foo2foo3 不会编译...
  • 是的,这没有意义,只有 A 类继承 enable_shared_from_this
  • 我认为你应该看看 enable_shared_from_this 的作用。看到这个question的答案@
  • 是的,这样更好:-)

标签: c++ c++11 shared-ptr multiple-inheritance enable-shared-from-this


【解决方案1】:

确实你做错了。如果你有简单的继承,只需从基类中的enable_shared_from this继承,派生类免费获取。 (当然你需要降低结果)

如果你有多重继承(看起来),你必须使用herehere 描述的技巧:

/* 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());
  }
};

那么你的代码就变成了:

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>(); 
    }
};

【讨论】:

  • 注意:派生类只能使用继承的 enable_shared_from_this 如果它是公共的,即:class BaseClass: public enable_shared_from_this&lt;BaseClass&gt;
  • @Offirmo 如果我无法修改(从inheritable_enable_shared_from_this 继承)基类(A、B、C),是否有任何可能的解决方案?
猜你喜欢
  • 1970-01-01
  • 2011-05-28
  • 2021-11-11
  • 2016-03-06
  • 2021-02-10
  • 1970-01-01
  • 2019-07-28
  • 2020-03-20
  • 1970-01-01
相关资源
最近更新 更多