楼主:
yhn0tgb60 (呦厚厚)
2016-01-26 16:04:31最近在学 C++11 smart pointer
发现除了 shared_ptr, weak_ptr,unique_ptr,
还有个 enable_shared_from_this,
但我不知道 enable_shared_from_this 的用途在哪???
好像主要是为了 shared_from_this
class base : public std::enable_shared_from_this<base> {};
shared_ptr<base> a= make_shared<base>();
shared_ptr<base> b=a->shared_from_this();
a中的count 会+1,
但是我不继承 enable_shared_from_this , 不使用 shared_from_this,
直接 shared_ptr<base> b=a;
也是一样的 a的 count +1, 不是吗???
那 enable_shared_from_this 的用途是????
另外 如果我 class base 继承了 enable_shared_from_this,
但是却 unique_ptr<base> a= make_unique<base>();
这样会发生什么事????
作者: firose (guest也是也是也是也是也) 2016-01-26 21:16:00
出来是 shared_ptr<base> 但仍然是指向 new1否则也可以 dynamic_pointer_cast 成 shared_ptr<new1>