• <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>
            posts - 23,  comments - 94,  trackbacks - 0
             1 #include <iostream>
             2 #include <boost/shared_ptr.hpp>
             3 #include <boost/enable_shared_from_this.hpp>
             4 
             5 using namespace std;
             6 
             7 class A : public boost::enable_shared_from_this<A>
             8 {
             9 public:
            10     boost::shared_ptr<A> child_;
            11     boost::shared_ptr<A> parent_;
            12 
            13     void add(boost::shared_ptr<A> child)
            14     {
            15         child_ = child;
            16         child_->set(shared_from_this());
            17     }
            18 
            19     void set(boost::shared_ptr<A> parent)
            20     {
            21         parent_ = parent;
            22     }
            23 };
            24 
            25 int main()
            26 {
            27     boost::shared_ptr<A> p1(new A);
            28     boost::shared_ptr<A> p2(new A);
            29 
            30     p1->add(p2);
            31 
            32     cout<<p1<<endl;
            33     cout<<p2<<endl;
            34     cout<<p1->child_<<endl;
            35     cout<<p2->parent_<<endl;
            36     cout<<p1.use_count()<<endl;
            37     cout<<p2.use_count()<<endl;
            38 
            39     return 0;
            40 }

            有了shared_from_this.. 我淚流滿面
            之前不知道這個(gè)的時(shí)候..用了很愚蠢的做法

            void add(shared_ptr<A> child)
                child_ = child;
                child_->set(shared_ptr<A>(this));
            }
            結(jié)果錯(cuò)誤連連~ 然后放棄使用shared_ptr... 用raw_ptr...

            順便推薦這本書
            beyond_stl_cn.chm

            放到了我的SVN上.. 一本很好的介紹Boost如何使用的書..

            http://code.google.com/p/charlib/source/browse/trunk/Boost%20Book/Beyond_STL_cn.rar

            進(jìn)入頁(yè)面后點(diǎn)右下的 view raw file 就可以下載了

            以上是早上寫的.. 寫好后很高興的發(fā)布了.. 但是后來發(fā)現(xiàn)上面這段程序非常的白癡
            最關(guān)鍵的就在于,其實(shí)這上面的兩個(gè)shared_ptr已經(jīng)循環(huán)引用了.. 再也沒有辦法自動(dòng)解開
            資源也就套死在了原地.. Oh My God... 居然愚蠢到這種地步..

            然后才發(fā)現(xiàn).. weak_ptr 一點(diǎn)都不weak.. 這里就需要用weak_ptr來處理!

            換成下面這個(gè)...
            #include <iostream>
            #include 
            <boost/shared_ptr.hpp>
            #include 
            <boost/weak_ptr.hpp>
            #include 
            <boost/enable_shared_from_this.hpp>

            using namespace std;

            class A : public boost::enable_shared_from_this<A>
            {
            public:
                A(
            const int id) : id_(id) { cout<<id_<<" Constructed!"<<endl; }
                
            ~A() { cout<<id_<<" Destructed!"<<endl; }

                
            int id_;
                boost::shared_ptr
            <A> child_;
                boost::weak_ptr
            <A> parent_;

                
            void add(boost::shared_ptr<A> child)
                {
                    child_ 
            = child;
                    child_
            ->set(shared_from_this());
                }

                
            void set(boost::shared_ptr<A> parent)
                {
                    parent_ 
            = parent;
                }

                boost::shared_ptr
            <A> get_parent()
                {
                    
            return parent_.lock();
                }
            };

            int main()
            {
                boost::shared_ptr
            <A> p1(new A(1));
                boost::shared_ptr
            <A> p2(new A(2));

                p1
            ->add(p2);

                
            return 0;
            }

            通過這個(gè)測(cè)試..
            輸出的結(jié)果是
            1 Construct
            2 Construct
            1 Destruct
            2 Destruct

            這樣的輸出并不奇怪. 因?yàn)?weak_ptr 是 shared_ptr 的觀察者,將 shared_ptr 傳給 weak_ptr 不會(huì)增加 shared_ptr的引用計(jì)數(shù). 所以這里的操作, p2 的引用計(jì)數(shù)是2, p1 的引用計(jì)數(shù)是1, 所以p1是unique的,p1先析構(gòu),p2的引用計(jì)數(shù)-1,然后析構(gòu).

            不過這里資源的析構(gòu)順序可能不是我們關(guān)心的范圍,我這里認(rèn)為把資源丟給智能指針這類物件管理后,主要是為了資源不泄漏,資源的析構(gòu)順序如果在關(guān)心的范圍,也就該自己管理該資源了.

            自己犯的一個(gè)低級(jí)錯(cuò)誤,趕忙把帖子存草稿了. 現(xiàn)在弄清楚怎么處理后,才敢發(fā)上來,呵呵~ ^ ^

            posted on 2009-03-12 19:20 Charlie 侯杰 閱讀(8950) 評(píng)論(4)  編輯 收藏 引用

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            by Charlie
            精品久久777| 亚洲欧美成人久久综合中文网 | 蜜桃麻豆www久久国产精品| 久久亚洲精品成人无码网站| 97精品国产91久久久久久| 亚洲国产成人久久一区久久| 久久狠狠爱亚洲综合影院| 国内精品欧美久久精品| 久久久久人妻一区精品性色av| 久久久精品国产Sm最大网站| 四虎国产精品免费久久久| 99re这里只有精品热久久| 久久国产成人精品国产成人亚洲| 国产成年无码久久久免费| 久久热这里只有精品在线观看| 久久亚洲欧美国产精品| 久久久久亚洲AV无码专区体验| 999久久久国产精品| 久久久久国产精品麻豆AR影院 | 蜜桃麻豆WWW久久囤产精品| 久久久久四虎国产精品| 区久久AAA片69亚洲| 亚洲欧美久久久久9999| 国内精品久久久久影院网站| 国产一区二区三区久久精品| 久久久久人妻一区精品色 | 人人狠狠综合久久亚洲婷婷| 午夜人妻久久久久久久久| 久久精品国产亚洲av日韩| 偷偷做久久久久网站| 久久久久亚洲精品中文字幕| 91精品国产91热久久久久福利| 国产精品青草久久久久婷婷| 久久国产精品成人影院| 久久无码人妻一区二区三区| 久久精品人妻中文系列| 区久久AAA片69亚洲| 亚洲αv久久久噜噜噜噜噜| 成人午夜精品无码区久久| 亚洲精品白浆高清久久久久久| 伊人久久综合无码成人网|