[問題] 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