• <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.. 我淚流滿面
            之前不知道這個的時候..用了很愚蠢的做法

            void add(shared_ptr<A> child)
                child_ = child;
                child_->set(shared_ptr<A>(this));
            }
            結果錯誤連連~ 然后放棄使用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

            進入頁面后點右下的 view raw file 就可以下載了

            以上是早上寫的.. 寫好后很高興的發布了.. 但是后來發現上面這段程序非常的白癡
            最關鍵的就在于,其實這上面的兩個shared_ptr已經循環引用了.. 再也沒有辦法自動解開
            資源也就套死在了原地.. Oh My God... 居然愚蠢到這種地步..

            然后才發現.. weak_ptr 一點都不weak.. 這里就需要用weak_ptr來處理!

            換成下面這個...
            #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;
            }

            通過這個測試..
            輸出的結果是
            1 Construct
            2 Construct
            1 Destruct
            2 Destruct

            這樣的輸出并不奇怪. 因為 weak_ptr 是 shared_ptr 的觀察者,將 shared_ptr 傳給 weak_ptr 不會增加 shared_ptr的引用計數. 所以這里的操作, p2 的引用計數是2, p1 的引用計數是1, 所以p1是unique的,p1先析構,p2的引用計數-1,然后析構.

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

            自己犯的一個低級錯誤,趕忙把帖子存草稿了. 現在弄清楚怎么處理后,才敢發上來,呵呵~ ^ ^

            posted on 2009-03-12 19:20 Charlie 侯杰 閱讀(8950) 評論(4)  編輯 收藏 引用
            by Charlie
            久久国产精品99国产精| 久久久久成人精品无码中文字幕| 东京热TOKYO综合久久精品| 久久久噜噜噜久久中文字幕色伊伊| 伊人色综合久久天天网| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 亚洲性久久久影院| 久久精品国产亚洲AV不卡| 久久国产精品-国产精品| 久久久久国产日韩精品网站| 午夜天堂精品久久久久| 国产成人无码精品久久久久免费| 热re99久久精品国99热| 色狠狠久久综合网| 91久久精品无码一区二区毛片| 久久伊人精品一区二区三区| 久久成人国产精品一区二区| 亚洲精品无码专区久久久| 一级a性色生活片久久无少妇一级婬片免费放 | 一本色道久久88综合日韩精品 | 国产精品九九久久精品女同亚洲欧美日韩综合区 | 日韩一区二区久久久久久| 日日噜噜夜夜狠狠久久丁香五月| 久久精品无码av| 久久人人爽人爽人人爽av| 亚洲色欲久久久久综合网| 久久无码一区二区三区少妇| 无码乱码观看精品久久| 久久99热这里只频精品6| 久久久精品2019免费观看| 久久久久亚洲av无码专区喷水| 久久精品成人国产午夜| 欧美无乱码久久久免费午夜一区二区三区中文字幕 | 色偷偷91久久综合噜噜噜噜| 久久综合亚洲鲁鲁五月天| 亚洲国产精品成人久久| 亚洲国产精品久久久久久| 亚洲国产小视频精品久久久三级| 久久久久久亚洲Av无码精品专口| 久久综合综合久久狠狠狠97色88| 一级A毛片免费观看久久精品|