【发布时间】:2012-07-08 11:51:38
【问题描述】:
与 Same address, multiple shared_ptr counters, is it forbidden by C++ standard? 和其他无数关于多个 shared_ptr 对象指向同一个对象但不共享底层引用计数结构的问题有关。
如果对象从上述问题中的“enable_shared_from_this”继承会发生什么?我的 shared_from_this() 返回什么?一个有自定义删除器还是一个没有?
struct B : boost::enable_shared_from_this<B> {
boost::weak_ptr < B > get_weak() {
return shared_from_this();
}
};
void doNothing(B *) {
}
int main() {
B * b0 = new B;
boost::shared_ptr < B > sddb0(b0, doNothing);
boost::weak_ptr < B > swddb0(sddb0->get_weak());
// Does this have a custom deleter???
boost::shared_ptr < B > sddb1 = swddb0.lock();
boost::shared_ptr < B > scdb0(b0);
boost::weak_ptr < B > swcdb0(sddb0->get_weak());
// Does this *not* have a custom deleter???
boost::shared_ptr < B > scdb1 = swcdb0.lock();
}
【问题讨论】:
标签: boost shared-ptr weak-ptr