• <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

             

            enable_shared_from_this(轉載)

            在 C++ 中需要自己來處理內存,稍微處理不當,就會存在非常郁悶的內存泄漏問題

             

            還好,現在 C++ 中推出了強大的智能指針,即 smart_ptr ,本文先稍微介紹一下 smart_ptr ,然后具體說說 shared_ptr 和 weak_ptr ,特別是 enable_shared_from_this 和 shared_from_this

             

            除了標準庫中的 auto_ptr 之外

            在 boost 或者 tr1 中的 smart_ptr 主要是有下面幾種

            • scoped_ptr
            • scoped_array
            • shared_ptr
            • shared_array
            • intrusive_ptr
            • weak_ptr

            這些里面最難理解的是綜合應用了 weak_ptr 和 shared_ptr 的 enable_shared_from_this 類,在該類中定了成員函數 shared_from_this() ,返回 shared_ptr<T> 。這個函數僅在 shared_ptr<T> 的構造函數被調用之后才能使用。原因是 enable_shared_from_this::weak_ptr 并不在構造函數中設置(此處的構造函數指的是類型 T 的構造函數),而是在 shared_ptr<T> 的構造函數中設置(此處的構造函數指的是類型 shared_ptr<T> 的構造函數)。

             

            在下面的代碼中:

             

            Cpp代碼
            1. #include <iostream>   
            2.   
            3. #include <string>   
            4.   
            5.     
            6.   
            7. #include <boost/shared_ptr.hpp>   
            8.   
            9. #include <boost/weak_ptr.hpp>   
            10.   
            11. #include <boost/enable_shared_from_this.hpp>   
            12.   
            13.     
            14.   
            15. using namespace std;   
            16.   
            17.     
            18.   
            19. struct Ansible   
            20.   
            21.   : public boost::enable_shared_from_this<Ansible>   
            22.   
            23. {   
            24.   
            25.     boost::shared_ptr<Ansible> get_shared()   
            26.   
            27.     {   
            28.   
            29.         boost::shared_ptr<Ansible> r(this);   
            30.   
            31.     
            32.   
            33.         return r;   
            34.   
            35.     }   
            36.   
            37.     
            38.   
            39.     ~Ansible()   
            40.   
            41.     {   
            42.   
            43.         cout<<"Destructor"<<endl;   
            44.   
            45.     }   
            46.   
            47. };   
            48.   
            49.     
            50.   
            51. int main(int argc,char* argv[])   
            52.   
            53. {   
            54.   
            55.     boost::shared_ptr<Ansible> a(new Ansible);   
            56.   
            57.     Ansible& r = *a;   
            58.   
            59.     //boost::shared_ptr<Ansible> b = r.get_shared();   
            60.   
            61.     boost::shared_ptr<Ansible> b = r.shared_from_this();   
            62.   
            63.     
            64.   
            65.     cout<<"Reference Number "<<a.use_count()<<" "<<b.use_count()<<endl;   
            66.   
            67.     
            68.   
            69.     return 0;   
            70.   
            71. }  
            #include <iostream> #include <string> #include <boost/shared_ptr.hpp> #include <boost/weak_ptr.hpp> #include <boost/enable_shared_from_this.hpp> using namespace std; struct Ansible : public boost::enable_shared_from_this<Ansible> { boost::shared_ptr<Ansible> get_shared() { boost::shared_ptr<Ansible> r(this); return r; } ~Ansible() { cout<<"Destructor"<<endl; } }; int main(int argc,char* argv[]) { boost::shared_ptr<Ansible> a(new Ansible); Ansible& r = *a; //boost::shared_ptr<Ansible> b = r.get_shared(); boost::shared_ptr<Ansible> b = r.shared_from_this(); cout<<"Reference Number "<<a.use_count()<<" "<<b.use_count()<<endl; return 0; }

             

            若不使用 shared_from_this() 成員函數,則會輸出 a 和 b 的 use_count() 都為 1 ,然后調用 2 次類型 Ansible 的析構函數,若添加了該成員函數,在 a 和 b 的 use_count() 輸出為 2 ,只是調用一次 Ansible 的析構函數。原因是 enable_shared_from_this 里面在 shared_ptr<T> 的時候構造了一個 weak_ptr 類,而 weak_ptr 只是監視,不增加引用計數

             

            (下面是轉載: http://huyuguang1976.spaces.live.com/blog/cns!2A9E272E3C33AFF1!185.entry

            所以如下代碼是錯誤的:

             

            class D:public boost::enable_shared_from_this<D>

            {

            public:

                D()

                {

                    boost::shared_ptr<D> p=shared_from_this();

                }

            };

             

            原因很簡單,在 D 的構造函數中雖然可以保證 enable_shared_from_this<D> 的構造函數已經被調用,但正如前面所說, weak_ptr 還沒有設置。

             

            如下代碼也是錯誤的:

             

            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();

            }

             

            錯誤原因同上。

             

            如下代碼是正確的:

             

            void main()

            {

                boost::shared_ptr<D> d(new D);

                d->func();

            }

             

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

             

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


            另解:::::
            struct X

            {

                     boost::shared_ptr<X> getX()

            {

                     boost::shared_ptr<X> r ;//????如何實現

                     return r;

            }

            };

             

            要得到X的智能指針,只是在對象指針是受shared_ptr保護的基礎上的,舉例如下:

            void test_X()

            {

                     {

            X x;

                              boost::shared_ptr<X> px = x.getX();//錯誤

            }

                     {

            X* x = new X();

            boost::shared_ptr<X> px = x->getX();//錯誤

            }

                     {

            boost::shared_ptr<X>  x (new X());

            boost::shared_ptr<X> px = x->getX();//正確

            }

            }

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

            導航

            統計

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            99久久综合狠狠综合久久止| 99re这里只有精品热久久| 九九99精品久久久久久| 欧美精品一本久久男人的天堂| 热99re久久国超精品首页| 精品国产青草久久久久福利| 久久综合久久性久99毛片| 久久精品99久久香蕉国产色戒| 国产成人99久久亚洲综合精品 | 亚洲日本va午夜中文字幕久久 | 久久精品中文字幕无码绿巨人| 狠狠色丁香久久综合婷婷| 久久久久国产一区二区| 久久婷婷久久一区二区三区| 亚洲国产精品狼友中文久久久 | 7777精品久久久大香线蕉| A级毛片无码久久精品免费| 久久伊人精品一区二区三区| 99久久亚洲综合精品网站| 久久婷婷五月综合色高清 | AV无码久久久久不卡网站下载| 色老头网站久久网| 久久e热在这里只有国产中文精品99 | 人妻无码精品久久亚瑟影视| 91亚洲国产成人久久精品| 久久天天躁夜夜躁狠狠躁2022| 久久亚洲国产成人影院| 久久最新精品国产| 久久国产精品成人免费| 久久夜色精品国产噜噜麻豆| 久久人人爽人人爽人人片AV不 | 国产毛片久久久久久国产毛片| 久久人爽人人爽人人片AV| 中文字幕久久波多野结衣av| 97视频久久久| 久久无码专区国产精品发布| 国产精品久久久香蕉| 久久强奷乱码老熟女网站| 久久福利资源国产精品999| 日本五月天婷久久网站| 久久天天躁夜夜躁狠狠躁2022|