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

            flushthink

            just for essential skill
            隨筆 - 14, 文章 - 0, 評論 - 30, 引用 - 0
            數據加載中……

            enable_shared_from_this、weak_ptr、shared_ptr

            網上說weak_ptr是shared_ptr的觀察員,weak_ptr不會干擾shared_ptr機制,當weak_ptr所觀察的shared_ptr要釋放對象時,weak_ptr的指針將被置空,避免空懸指針。
            weak_ptr只能通過shared_ptr或weak_ptr構造。

            對于一個shared_ptr,它分別對強引用和弱引用都做了計數。

            上圖是下面代碼的調試信息。
            #include "stdafx.h"
            #include 
            <iostream>
            #include 
            <boost/shared_ptr.hpp>
            #include 
            <boost/weak_ptr.hpp>
            #include 
            <boost/enable_shared_from_this.hpp>

            struct A
            {};

            int _tmain(int argc, _TCHAR* argv[])
            {
                boost::shared_ptr
            <A> a(new A);

                
            return 0;
            }

            當一個shared_ptr構造,use_count_和weak_count_都被賦值為1。



            上圖是下面代碼調試信息。
            #include "stdafx.h"
            #include 
            <iostream>
            #include 
            <boost/shared_ptr.hpp>
            #include 
            <boost/weak_ptr.hpp>
            #include 
            <boost/enable_shared_from_this.hpp>

            struct A
            {};

            class B 
            {
            public:
                B()
                    : mA(
            new A)
                
            {
                }


                
            /// 把指針返回出去
                boost::shared_ptr<A> get()
                
            {
                    
            return mA;
                }

            private:
                boost::shared_ptr
            <A> mA;
            }
            ;

            int _tmain(int argc, _TCHAR* argv[])
            {
                
            {
                    B b;
                    boost::weak_ptr
            <A> wp1 = b.get();
                    boost::weak_ptr
            <A> wp2 = b.get();
                    boost::shared_ptr
            <A> sp1 = b.get();
                    boost::shared_ptr
            <A> sp2 = b.get();
                }


                
            return 0;
            }



            當一個對象返回出一個自身對象shared_ptr,一般用enable_shared_from_this,而enable_shared_from_this就是用weak_ptr來實現的。

             

            #include "stdafx.h"
            #include 
            <iostream>
            #include 
            <boost/shared_ptr.hpp>
            #include 
            <boost/weak_ptr.hpp>
            #include 
            <boost/enable_shared_from_this.hpp>

            struct A : public boost::enable_shared_from_this<A>
            {

                boost::shared_ptr
            <A> get()
                
            {
                    boost::shared_ptr
            <A> holder(new A);
                    
            return holder;
                }

            }
            ;

            int _tmain(int argc, _TCHAR* argv[])
            {
                
                A a;
                boost::shared_ptr
            <A> aa = a.get();
                
            return 0;
            }

            上述代碼的對象構造是:enable_shared_from_this<A>,然后是A,再就是shared_ptr,所以必須在構造完成之后再進行對象智能指針的創建。這是一種循環依賴關系,一般循環依賴都要用到weak_ptr。

            針對循環依賴的shared_ptr如下:
            #include "stdafx.h"
            #include 
            <iostream>
            #include 
            <boost/shared_ptr.hpp>
            #include 
            <boost/weak_ptr.hpp>
            #include 
            <boost/enable_shared_from_this.hpp>

            /// 相互依賴
            struct Product;
            struct ProductManager;

            typedef boost::shared_ptr
            <Product> ProductPtr;
            typedef boost::shared_ptr
            <ProductManager> ProductManagerPtr;

            struct Product
            {
                Product()
                
            {
                    std::cout 
            << "Product 構造" << std::endl;
                }

                
            ~Product()
                
            {
                    std::cout 
            << "Product 析構" << std::endl;
                }

                ProductManagerPtr mMgr;
            }
            ;

            struct ProductManager
            {
                ProductManager()
                
            {
                    std::cout 
            << "ProductManager 構造" << std::endl;
                }

                
            ~ProductManager()
                
            {
                    std::cout 
            << "ProductManager 析構" << std::endl;
                }

                ProductPtr mProduct;
            }
            ;

            int _tmain(int argc, _TCHAR* argv[])
            {
                ProductPtr product(
            new Product);
                ProductManagerPtr productMgr(
            new ProductManager);
                product
            ->mMgr = productMgr;
                productMgr
            ->mProduct = product;
                
            return 0;
            }

            結果是:


            不能釋放對象。
            對于這種情況要這么做:

            struct Product
            {
                Product()
                
            {
                    std::cout 
            << "Product 構造" << std::endl;
                }

                
            ~Product()
                
            {
                    std::cout 
            << "Product 析構" << std::endl;
                }

                boost::weak_ptr
            <ProductManager> mMgr;
            }
            ;


             

            posted on 2009-09-17 15:14 tiny 閱讀(2118) 評論(0)  編輯 收藏 引用

            久久99精品久久久久久不卡| 久久青草国产手机看片福利盒子| 97超级碰碰碰碰久久久久| 国产精品九九九久久九九| 77777亚洲午夜久久多喷| 国产高潮久久免费观看| 久久亚洲国产精品123区| 伊人久久大香线蕉精品不卡| 亚洲中文精品久久久久久不卡| 亚洲午夜久久久久妓女影院 | 亚洲精品无码专区久久久| 亚洲av成人无码久久精品| 亚洲国产精品久久久久婷婷软件| 久久国产精品久久久| 久久性生大片免费观看性| 色婷婷综合久久久久中文一区二区 | 精品熟女少妇av免费久久| 91精品国产综合久久香蕉 | 久久天天躁狠狠躁夜夜avapp| 久久精品亚洲日本波多野结衣| 99久久伊人精品综合观看| 亚洲午夜无码AV毛片久久| 97久久超碰国产精品2021| 国产精品久久久久久久久久影院 | 日本免费久久久久久久网站| 亚洲一区精品伊人久久伊人 | 久久久这里只有精品加勒比| 国产成年无码久久久久毛片| 综合久久一区二区三区| 国产99久久久国产精免费| 99久久国产综合精品女同图片| 丰满少妇人妻久久久久久| 蜜桃麻豆WWW久久囤产精品| 国产激情久久久久影院小草| 久久99热精品| 91精品国产高清91久久久久久| 99蜜桃臀久久久欧美精品网站| 日日狠狠久久偷偷色综合96蜜桃| 91精品国产91热久久久久福利| 国产精品视频久久| 国产亚洲综合久久系列|