[问题] enable_shared_from_this 的功能???

楼主: 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>();
这样会发生什么事????
作者: suhorng ( )   2016-01-26 16:14:00
不一样的点例如, 在 base 里面时看不到 a 呀
作者: LiloHuang (十年一刻)   2016-01-26 20:54:00
我想重点在于这个例子 http://goo.gl/UkbKBR
作者: firose (guest也是也是也是也是也)   2016-01-26 21:16:00
出来是 shared_ptr<base> 但仍然是指向 new1否则也可以 dynamic_pointer_cast 成 shared_ptr<new1>
作者: bibo9901 (function(){})()   2016-01-27 14:11:00
我看不懂你把A的实作隐藏后,再于B公开的用意?是pimpl吗? 也不太像

Links booklink

Contact Us: admin [ a t ] ucptt.com