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

            colorful

            zc qq:1337220912

             

            why enable_shared_from_this

             為什么會有 tr1::enable_shared_from_this 這個類,一直不解,直到看了Stephan T. Lavavej給出的關于shared_ptr 的PPT。 Stephan T. Lavavej是Visual C++ Libraries Developer。

            他給的例子是:

            struct Ansible {
                shared_ptr<Ansible> get_shared() {
                    shared_ptr<Ansible> ret(this);
                    return ret;
                }
            };

            int main() {
                shared_ptr<Ansible> a(new Ansible);
                Ansible& r = *a;
                shared_ptr<Ansible> b = r.get_shared();
            }

            Result: DOUBLE DELETION


            然后使用enable_shared_from_this 就對了:

            struct Ansible
                : public enable_shared_from_this<Ansible> { };

            int main() {
                shared_ptr<Ansible> a(new Ansible);
                Ansible& r = *a;
                shared_ptr<Ansible> b = r.shared_from_this();
            }

            a and b share ownership, as if:
            shared_ptr<Ansible> b = a;



            為什么?看看enable_shared_from_this 的實現://也可看boost

            template<class _Ty> class enable_shared_from_this
             { // provide member functions that create shared_ptr to this
            public:
             typedef _Ty _EStype;

             shared_ptr<_Ty> shared_from_this()
              { // return shared_ptr
              return shared_ptr<_Ty>(_Wptr);
              }

             shared_ptr<const _Ty> shared_from_this() const
              { // return shared_ptr
              return shared_ptr<const _Ty>(_Wptr);
              }

            protected:
             enable_shared_from_this()
              { // construct (do nothing)
              }

             enable_shared_from_this(const enable_shared_from_this& _Right)
              : _Wptr(_Right._Wptr)
              { // construct
              }

             enable_shared_from_this& operator=(const enable_shared_from_this& _Right)
              { // assign
              _Wptr = _Right._Wptr;
              return *this;
              }

             ~enable_shared_from_this()
              { // destroy (do nothing)
              }

            private:
             template<class _Ty1,
              class _Ty2>
              friend void _Do_enable(
               _Ty1 *,
               enable_shared_from_this<_Ty2>*,
               _Ref_count_base *);

             mutable weak_ptr<_Ty> _Wptr;
             };


            enable_shared_from_this 的member可是一個weak_ptr.

            當然,從enable_shared_from_this繼承后,遇到shared_ptr后,member _Wptr 是怎樣工作的,另有玄機。 可以設斷點在 _Do_enable 里,有助于理解。

            看似簡單的share_ptr, 被實現的看似危機四伏。

            posted on 2012-04-07 11:57 多彩人生 閱讀(425) 評論(0)  編輯 收藏 引用

            導航

            統計

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            狠狠色婷婷综合天天久久丁香 | 久久人人添人人爽添人人片牛牛 | 亚洲综合伊人久久大杳蕉| 亚洲日韩欧美一区久久久久我| 精品久久久久久中文字幕| 国产成人无码精品久久久性色| 亚洲精品乱码久久久久久久久久久久| 久久91精品国产91| 亚洲欧美国产日韩综合久久| 7777精品久久久大香线蕉| 国产叼嘿久久精品久久| 欧美精品九九99久久在观看| 国产精品岛国久久久久| 亚洲国产日韩欧美久久| 欧美一区二区精品久久| 一本色道久久99一综合| 久久国产成人午夜AV影院| 久久精品国产亚洲AV香蕉| 久久午夜福利电影| 97久久精品午夜一区二区| 亚洲狠狠婷婷综合久久蜜芽| 久久久久久无码国产精品中文字幕| 久久久久亚洲AV无码专区体验| 久久久久中文字幕| 亚洲va久久久噜噜噜久久狠狠| 国内精品欧美久久精品| 国产精品久久久久无码av| 性做久久久久久久久| 久久成人永久免费播放| 国产高潮国产高潮久久久91| 久久精品国产第一区二区三区| 国产偷久久久精品专区| 久久男人AV资源网站| 国产精品热久久毛片| 国产成人无码精品久久久久免费 | 亚洲AV无码久久| 亚洲精品无码久久久| 亚洲国产成人精品久久久国产成人一区二区三区综 | 久久久无码精品亚洲日韩软件| 精品久久久久久中文字幕| 国产成人久久精品一区二区三区 |