欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
shared_from_this 幾個(gè)值得注意的地方
shared_from_this()是enable_shared_from_this<T>的成員函數,返回shared_ptr<T>。首先需要注意的是,這個(gè)函數僅在shared_ptr<T>的構造函數被調用之后才能使用。原因是enable_shared_from_this::weak_ptr并不在構造函數中設置,而是在shared_ptr<T>的構造函數中設置。

如下代碼是錯誤的:
  1. class D:public boost::enable_shared_from_this<D>
  2. {
  3. public:
  4.     D()
  5.     {
  6.         boost::shared_ptr<D> p=shared_from_this();
  7.     }
  8. };
原因很簡(jiǎn)單,在D的構造函數中雖然可以保證enable_shared_from_this<D>的構造函數已經(jīng)被調用,但正如前面所說(shuō),weak_ptr還沒(méi)有設置。

如下代碼也是錯誤的:
  1. class D:public boost::enable_shared_from_this<D>
  2. {
  3. public:
  4.     void func()
  5.     {
  6.         boost::shared_ptr<D> p=shared_from_this();
  7.     }
  8. };

  9. void main()
  10. {
  11.     D d;
  12.     d.func();
  13. }
錯誤原因同上。

如下代碼是正確的:
  1. void main()
  2. {
  3.     boost::shared_ptr<D> d(new D);
  4.     d->func();
  5. }
這里boost::shared_ptr<D> d(new D)實(shí)際上執行了3個(gè)動(dòng)作:首先調用enable_shared_from_this<D>的構造函數;其次調用D的構造函數;最后調用shared_ptr<D>的構造函數。是第3個(gè)動(dòng)作設置了enable_shared_from_this<D>的weak_ptr,而不是第1個(gè)動(dòng)作。這個(gè)地方是很違背c++常理和邏輯的,必須小心。

結論是,不要在構造函數中使用shared_from_this;其次,如果要使用shared_ptr,則應該在所有地方均使用,不能使用D d這種方式,也決不要傳遞裸指針。   

另一個(gè)值得注意的地方是在類(lèi)的繼承樹(shù)中不能有2個(gè)或更多個(gè)enable_shared_from_this<T>。例如如下代碼是錯誤的:
  1. class A:public boost::enable_shared_from_this<A>
  2. {
  3. public:
  4.     A():a(1){}
  5.     virtual ~A(){}
  6.     boost::shared_ptr<A> get_ptra(){return shared_from_this();}
  7.     int a;
  8. };

  9. class B:public A,public boost::enable_shared_from_this<B>
  10. {
  11. public:
  12.     B():b(2){}
  13.     boost::shared_ptr<B> get_ptrb()
  14.     {
  15.         return boost::enable_shared_from_this<B>::shared_from_this();
  16.     }
  17.     int b;
  18. };

  19. int _tmain(int argc, _TCHAR* argv[])
  20. {
  21.     {
  22.         boost::shared_ptr<B> x(new B);
  23.         boost::shared_ptr<A> a1 = x->get_ptra();
  24.         boost::shared_ptr<B> b1 = x->get_ptrb();
  25.     }

  26.     return 0;
  27. }
注意上面代碼中,B同時(shí)擁有2個(gè)enable_shared_from_this的基類(lèi),一個(gè)是enable_shared_from_this<A>,另一個(gè)是enable_shared_from_this<B>。在boost::shared_ptr<B> x(new B);這行代碼中,shared_ptr<B>的構造函數僅會(huì )設置2個(gè)基類(lèi)中的一個(gè)的weak_ptr。在上面的例子中,僅設置enable_shared_from_this<A>的。如果修改B的定義為:

class B:public boost::enable_shared_from_this<B>,public A,

則僅設置enable_shared_from_this<B>的weak_ptr。很明顯都是錯誤的。

那么enable_shared_from_this以及shared_ptr為何要如此實(shí)現呢?又為什么會(huì )有如此怪異的結果呢?

首先考察shared_ptr的構造函數:
  1. template<class Y>
  2. explicit shared_ptr( Y * p ): px( p ), pn( p ) // Y must be complete
  3. {
  4.     boost::detail::sp_enable_shared_from_this( pn, p, p );
  5. }

  6. template<class T, class Y> void sp_enable_shared_from_this( shared_count const & pn, boost::enable_shared_from_this<T> const * pe, Y const * px )
  7. {
  8.     if(pe != 0) pe->_internal_weak_this._internal_assign(const_cast<Y*>(px), pn);
  9. }
注意這個(gè)sp_enable_shared_from_this是一個(gè)模板函數,而且僅調用了一次,所以不可能2個(gè)enable_shared_from_this基類(lèi)的weak_ptr都被賦值。但問(wèn)題在于,在調換了B的定義之后結果居然是不一樣的。這里有一個(gè)很隱秘的編譯器BUG。按道理來(lái)說(shuō),編譯器在編譯這段代碼時(shí),應該注意到無(wú)法真正決斷該怎么實(shí)例化sp_enable_shared_from_this并且報一個(gè)錯,但vc 2008并沒(méi)有報錯,而是通過(guò)編譯了。(g++會(huì )在此處報錯)

那么正確的解法是怎樣的呢?
  1. class B:public A
  2. {
  3. public:
  4.     B():b(2){}
  5.     boost::shared_ptr<B> get_ptrb()
  6.     {
  7.         return boost::dynamic_pointer_cast<B>(shared_from_this());
  8.     }
  9.     int b;
  10. };
注意到這里B并沒(méi)有直接繼承enable_shared_from_this,而是使用dynamic_pointer_cast進(jìn)行了類(lèi)型轉換。

關(guān)于為什么enable_shared_from_this是這樣實(shí)現的,可以參看作者原文:

Every enable_shared_from_this base contains a weak_ptr, The shared_ptr constructor looks up the enable_shared_from_this base and initializes its weak_ptr accordingly. This doesn't work when there are
two or more enable_shared_from_this bases, though.

I could put the weak_ptr in a virtual polymorphic base. This would force polymorphism on all clients of enable_shared_from_this... probably acceptable. It will also force a dynamic_pointer_cast in every
shared_from_this, and this may be harder to swallow, particularly in cases where RTTI is off. So I'm not sure.

If you do want the above behavior, it's easy to duplicate, as I already responded in my first post on the topic. Just make FooB return dynamic_pointer_cast<B>( FooA() ) and remove the enable_shared_from_this<B>
base (A needs to be made polymorphic, of course).

注意為了讓dynamic_pointer_cast能工作,A必須具有虛函數,那么最簡(jiǎn)單的做法當然是令其析構函數為虛函數(通常一個(gè)class如果希望被繼承,析構函數就應該為虛函數)。
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
Boost智能指針
使用enable_shared_from_this
C 異步調用時(shí)boost的 share
enable_shared_from_this 實(shí)現
boost::share_ptr用法
【轉】C++ 智能指針詳解
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久