• <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
            數(shù)據(jù)加載中……

            enable_shared_from_this、weak_ptr、shared_ptr

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

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

            上圖是下面代碼的調(diào)試信息。
            #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。



            上圖是下面代碼調(diào)試信息。
            #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來實現(xiàn)的。

             

            #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,所以必須在構造完成之后再進行對象智能指針的創(chuàng)建。這是一種循環(huán)依賴關系,一般循環(huán)依賴都要用到weak_ptr。

            針對循環(huán)依賴的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 閱讀(2127) 評論(0)  編輯 收藏 引用

            亚洲婷婷国产精品电影人久久| 国产99精品久久| 国产午夜免费高清久久影院| 久久婷婷五月综合成人D啪| 精品久久久无码人妻中文字幕豆芽| 亚洲va久久久噜噜噜久久天堂| 精品国产VA久久久久久久冰| 久久久久久国产精品美女| 久久人妻少妇嫩草AV蜜桃| 国内精品久久久久| 伊人热热久久原色播放www| 麻豆一区二区99久久久久| 国产精品免费久久久久影院| 亚洲精品无码久久一线| 品成人欧美大片久久国产欧美| 无码国内精品久久综合88 | 久久久91人妻无码精品蜜桃HD| yy6080久久| 久久91这里精品国产2020| 亚洲伊人久久大香线蕉综合图片| 久久―日本道色综合久久| 亚洲AV无码久久精品蜜桃| 久久伊人中文无码| 国产ww久久久久久久久久| 国内精品久久久久伊人av| 久久久噜噜噜久久中文字幕色伊伊 | 久久国产免费直播| 久久精品视屏| 国产高潮国产高潮久久久91| 欧洲人妻丰满av无码久久不卡| 久久久久亚洲国产| 东方aⅴ免费观看久久av| 欧美一级久久久久久久大片| 久久e热在这里只有国产中文精品99| 久久99国产综合精品免费| 久久亚洲精品中文字幕| 一本久久知道综合久久| 人妻丰满AV无码久久不卡| 亚洲国产精品无码久久| 97精品国产97久久久久久免费 | 国产亚洲精品久久久久秋霞|