• <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)  編輯 收藏 引用

            亚洲国产精品嫩草影院久久| 久久免费线看线看| 女人高潮久久久叫人喷水| 一级A毛片免费观看久久精品| 久久这里都是精品| 亚洲国产欧美国产综合久久| 国产亚洲欧美精品久久久| 国产午夜福利精品久久| 亚洲伊人久久综合中文成人网| 久久国语露脸国产精品电影| 国产欧美久久久精品| 热RE99久久精品国产66热| 久久精品亚洲一区二区三区浴池 | 青青青国产成人久久111网站| 伊人久久综在合线亚洲2019 | 久久狠狠爱亚洲综合影院| 99久久免费国产精精品| 思思久久99热免费精品6| 国产精品对白刺激久久久| 性欧美大战久久久久久久| 国产精品99久久精品| 久久精品视频一| 九九热久久免费视频| 久久综合给合久久国产免费| 久久噜噜久久久精品66| 久久99国产亚洲高清观看首页| 久久精品一本到99热免费| 国产一区二区精品久久岳| 久久国产色AV免费看| 97精品伊人久久大香线蕉| 久久国产热这里只有精品| 国产一级做a爰片久久毛片| 国产精品一区二区久久国产| 久久www免费人成看片| 婷婷国产天堂久久综合五月| 久久午夜福利电影| 久久久受www免费人成| 久久精品国产欧美日韩| 久久97久久97精品免视看| 国产精品欧美久久久久无广告 | 久久久无码精品亚洲日韩按摩|