• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            colorful

            zc qq:1337220912

             

            shared_from_this 幾個值得注意的地方

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

            如下代碼是錯誤的:
            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. };
            復制代碼
            原 因很簡單,在D的構造函數(shù)中雖然可以保證enable_shared_from_this<D>的構造函數(shù)已經(jīng)被調用,但正如前面所 說,weak_ptr還沒有設置。

            如下代碼也是錯誤的:
            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)實際上執(zhí)行了3個動作:首先調用enable_shared_from_this<D>的構造函數(shù);其次調用D的構造函數(shù);最后調用 shared_ptr<D>的構造函數(shù)。是第3個動作設置了enable_shared_from_this<D>的 weak_ptr,而不是第1個動作。這個地方是很違背c++常理和邏輯的,必須小心。

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

            另一個值得注意的地方是在類的繼承樹中不能有2個或更多個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同時擁有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>的構造函數(shù)僅會設置2個基類中的一個的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為何要如此實現(xiàn)呢?又為什么會有如此怪異的結果呢?

            首先考察shared_ptr的構造函數(shù):
            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. }
            復制代碼
            注 意這個sp_enable_shared_from_this是一個模板函數(shù),而且僅調用了一次,所以不可能2個 enable_shared_from_this基類的weak_ptr都被賦值。但問題在于,在調換了B的定義之后結果居然是不一樣的。這里有一個很隱 秘的編譯器BUG。按道理來說,編譯器在編譯這段代碼時,應該注意到無法真正決斷該怎么實例化sp_enable_shared_from_this并且 報一個錯,但vc 2008并沒有報錯,而是通過編譯了。(g++會在此處報錯)

            那么正確的解法是怎樣的呢?
            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并沒有直接繼承enable_shared_from_this,而是使用dynamic_pointer_cast進行了類型轉換。

            關于為什么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ù),那么最簡單的做法當然是令其析構函 數(shù)為虛函數(shù)(通常一個class如果希望被繼承,析構函數(shù)就應該為虛函數(shù))

            posted on 2012-06-22 22:42 多彩人生 閱讀(630) 評論(0)  編輯 收藏 引用 所屬分類: boost

            導航

            統(tǒng)計

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久精品国产91久久麻豆自制| 欧洲人妻丰满av无码久久不卡| 51久久夜色精品国产| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 久久国产乱子精品免费女| 国产精品久久久久久福利漫画 | 国产精品久久久久影院嫩草| 大美女久久久久久j久久| 久久午夜无码鲁丝片秋霞| 999久久久免费精品国产| 久久亚洲电影| 999久久久免费精品国产| 亚洲欧洲久久久精品| 久久精品成人免费网站| 中文字幕乱码久久午夜| 久久成人国产精品一区二区| 伊人久久大香线蕉亚洲| 久久成人精品| 久久国产午夜精品一区二区三区| 无码人妻久久一区二区三区| 久久精品免费大片国产大片 | 久久无码一区二区三区少妇 | 久久久久国产视频电影| 97久久精品无码一区二区| 久久婷婷午色综合夜啪| 国产精品欧美久久久久天天影视| 婷婷五月深深久久精品| 久久中文字幕人妻熟av女| 久久综合精品国产一区二区三区| 青青青青久久精品国产 | 亚洲v国产v天堂a无码久久| 国产成人综合久久精品尤物| 91久久精一区二区三区大全| 老色鬼久久亚洲AV综合| 亚洲中文字幕无码久久精品1| 久久久久久久久久久久久久| 一本久久精品一区二区| 亚洲精品乱码久久久久久蜜桃| 性高朝久久久久久久久久| 久久婷婷人人澡人人| 色综合久久88色综合天天 |