作者:
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>