日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Shared_from_this 几个值得注意的地方

發(fā)布時間:2023/12/10 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Shared_from_this 几个值得注意的地方 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

shared_from_this()是enable_shared_from_this<T>的成員 函數(shù),返回shared_ptr<T>。首先需要注意的是,這個函數(shù)僅在shared_ptr<T>的構(gòu)造函數(shù)被調(diào)用之后才能使 用。原因是enable_shared_from_this::weak_ptr并不在構(gòu)造函數(shù)中設(shè)置,而是在shared_ptr<T>的 構(gòu)造函數(shù)中設(shè)置。?

如下代碼是錯誤的:

  • class D:public?boost::enable_shared_from_this<D>
  • {
  • public:
  • ? ? D()
  • ? ? {
  • ? ?? ???boost::shared_ptr<D> p=shared_from_this();
  • ? ? }
  • };
  • 復(fù)制代碼

    原 因很簡單,在D的構(gòu)造函數(shù)中雖然可以保證enable_shared_from_this<D>的構(gòu)造函數(shù)已經(jīng)被調(diào)用,但正如前面所 說,weak_ptr還沒有設(shè)置。?

    如下代碼也是錯誤的:

  • class D:public boost::enable_shared_from_this<D>
  • {
  • public:
  • ? ? void func()
  • ? ? {
  • ? ?? ???boost::shared_ptr<D> p=shared_from_this();
  • ? ? }
  • };
  • void main()
  • {
  • ? ? D d;
  • ? ? d.func();
  • }
  • 復(fù)制代碼

    錯 誤原因同上。?

    如下代碼是正確的:

  • void main()
  • {
  • ? ? boost::shared_ptr<D> d(new D);
  • ? ? d->func();
  • }
  • 復(fù)制代碼

    這 里boost::shared_ptr<D> d(new D)實際上執(zhí)行了3個動作:首先調(diào)用enable_shared_from_this<D>的構(gòu)造函數(shù);其次調(diào)用D的構(gòu)造函數(shù);最后調(diào)用 shared_ptr<D>的構(gòu)造函數(shù)。是第3個動作設(shè)置了enable_shared_from_this<D>的 weak_ptr,而不是第1個動作。這個地方是很違背c++常理和邏輯的,必須小心。?

    結(jié)論是,不要在構(gòu)造函數(shù)中使用shared_from_this;其次,如果要使用shared_ptr,則應(yīng)該 在所有地方均使用,不能使用D d這種方式,也決不要傳遞裸指針。???

    另一個值得注意的地方是在類的繼承樹中不能有2個或更多個enable_shared_from_this<T>。例如如下代碼是錯誤的:

  • class A:public boost::enable_shared_from_this<A>
  • {
  • public:
  • ? ? A():a(1){}
  • ? ? virtual ~A(){}
  • ? ? boost::shared_ptr<A> get_ptra(){return shared_from_this();}
  • ? ? int a;
  • };
  • class B:public A,public boost::enable_shared_from_this<B>
  • {
  • public:
  • ? ? B():b(2){}
  • ? ? boost::shared_ptr<B> get_ptrb()
  • ? ? {
  • ? ?? ???return boost::enable_shared_from_this<B>::shared_from_this();
  • ? ? }
  • ? ? int b;
  • };
  • int _tmain(int argc, _TCHAR* argv[])
  • {
  • ? ? {
  • ? ?? ???boost::shared_ptr<B> x(new B);
  • ? ?? ???boost::shared_ptr<A> a1 = x->get_ptra();
  • ? ?? ???boost::shared_ptr<B> b1 = x->get_ptrb();
  • ? ? }
  • ? ? return 0;
  • }
  • 復(fù)制代碼

    注 意上面代碼中,B同時擁有2個enable_shared_from_this的基類,一個是 enable_shared_from_this<A>,另一個是enable_shared_from_this<B>。在 boost::shared_ptr<B> x(new B);這行代碼中,shared_ptr<B>的構(gòu)造函數(shù)僅會設(shè)置2個基類中的一個的weak_ptr。在上面的例子中,僅設(shè)置 enable_shared_from_this<A>的。如果修改B的定義為:?

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

    則僅設(shè)置enable_shared_from_this<B>的weak_ptr。很明顯都是錯誤的。?

    那么enable_shared_from_this以及shared_ptr為何要如此實現(xiàn)呢?又為什么會有如此怪異的結(jié)果呢??

    首先考察shared_ptr的構(gòu)造函數(shù):

  • template<class Y>
  • explicit shared_ptr( Y * p ): px( p ), pn( p ) // Y must be complete
  • {
  • ? ? boost::detail::sp_enable_shared_from_this( pn, p, p );
  • }
  • 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 )
  • {
  • ? ? if(pe != 0) pe->_internal_weak_this._internal_assign(const_cast<Y*>(px), pn);
  • }
  • 復(fù)制代碼

    注 意這個sp_enable_shared_from_this是一個模板函數(shù),而且僅調(diào)用了一次,所以不可能2個 enable_shared_from_this基類的weak_ptr都被賦值。但問題在于,在調(diào)換了B的定義之后結(jié)果居然是不一樣的。這里有一個很隱 秘的編譯器BUG。按道理來說,編譯器在編譯這段代碼時,應(yīng)該注意到無法真正決斷該怎么實例化sp_enable_shared_from_this并且 報一個錯,但vc 2008并沒有報錯,而是通過編譯了。(g++會在此處報錯)?

    那么正確的解法是怎樣的呢?

  • class B:public A
  • {
  • public:
  • ? ? B():b(2){}
  • ? ? boost::shared_ptr<B> get_ptrb()
  • ? ? {
  • ? ?? ???return boost::dynamic_pointer_cast<B>(shared_from_this());
  • ? ? }
  • ? ? int b;
  • };
  • 復(fù)制代碼

    注 意到這里B并沒有直接繼承enable_shared_from_this,而是使用dynamic_pointer_cast進(jìn)行了類型轉(zhuǎn)換。?

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

    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必須具有虛函數(shù),那么最簡單的做法當(dāng)然是令其析構(gòu)函 數(shù)為虛函數(shù)(通常一個class如果希望被繼承,析構(gòu)函數(shù)就應(yīng)該為虛函數(shù))。

    轉(zhuǎn)載于:https://www.cnblogs.com/rooney/archive/2013/03/31/2992101.html

    總結(jié)

    以上是生活随笔為你收集整理的Shared_from_this 几个值得注意的地方的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。