【发布时间】:2014-12-16 13:52:13
【问题描述】:
最近,我遇到了一个关于 boost 智能指针的问题。要指定,enable_shared_from_this 为类 T 保留一个“this”shared_ptr。当 T 的实例超出范围时,enable_shared_from_this 仍然存在。但是我创建一个原始 ptr 的 shared_ptr 的新对象呢?又有什么区别呢?
非常感谢。
class Foo : public boost::enable_shared_from_this<Foo>
{};
int main() {
shared_ptr<Foo> p1(new Foo);
shared_ptr<Foo> p2 = p1->shared_from_this();
shared_ptr<Foo> p3 = shared_ptr<Foo>(p1);
p1.reset();
// p1 is released, p2 and p3 still remains.
// but what is the diff between p2 and p3?
// Can I say shared_from_this() works the same way as
// shared_ptr<Foo>(p1);
}
【问题讨论】:
标签: c++ boost shared-ptr