【发布时间】: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 调用?
【问题讨论】:
-
我认为
foo2或foo3不会编译... -
是的,这没有意义,只有 A 类继承 enable_shared_from_this
-
我认为你应该看看 enable_shared_from_this 的作用。看到这个question的答案@
-
是的,这样更好:-)
标签: c++ c++11 shared-ptr multiple-inheritance enable-shared-from-this