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

               C++ 技術(shù)中心

               :: 首頁 :: 聯(lián)系 ::  :: 管理
              160 Posts :: 0 Stories :: 87 Comments :: 0 Trackbacks

            公告

            鄭重聲明:本BLOG所發(fā)表的原創(chuàng)文章,作者保留一切權(quán)利。必須經(jīng)過作者本人同意后方可轉(zhuǎn)載,并注名作者(天空)和出處(CppBlog.com)。作者Email:coder@luckcoder.com

            留言簿(27)

            搜索

            •  

            最新隨筆

            最新評論

            評論排行榜

            使用enable_shared_from_this

            說明
            The header <boost/enable_shared_from_this.hpp> defines the class template enable_shared_from_this. It is used as a base class that allows a shared_ptr to the current object to be obtained from within a member function.
            繼承該類就可以進(jìn)行基于當(dāng)前子類進(jìn)行安全的weap_ptr到shared_ptr的轉(zhuǎn)換...

            代碼實例
            以下代碼中Y類繼承enable_shared_from_this,, 從而我們可以直接在函數(shù)中調(diào)用shared_from_this獲得該對象的shared_ptr

            class Y: public enable_shared_from_this<Y>
            {
            public:

                shared_ptr<Y> f()
                {
                    return shared_from_this();
                }
            }

            int main()
            {
                shared_ptr<Y> p(new Y);
            // 調(diào)用f獲得shared_ptr

                shared_ptr<Y> q = p->f();
                assert(p == q);
                assert(!(p < q || q < p)); // p and q must share ownership

            }


            該類的實現(xiàn)


            template<class T> class enable_shared_from_this
            {
            protected:

                enable_shared_from_this()
                {
                }

                enable_shared_from_this(enable_shared_from_this const &)
                {
                }

                enable_shared_from_this & operator=(enable_shared_from_this const &)
                {
                    return *this;
                }

                ~enable_shared_from_this()
                {
                }

            public:

                shared_ptr<T> shared_from_this()
                {
                    shared_ptr<T> p(_internal_weak_this);
                    BOOST_ASSERT(p.get() == this);
                    return p;
                }

                shared_ptr<T const> shared_from_this() const
                {
                    shared_ptr<T const> p(_internal_weak_this);
                    BOOST_ASSERT(p.get() == this);
                    return p;
                }

            // Note: No, you don't need to initialize _internal_weak_this

            //

            // Please read the documentation, not the code

            //

            // http://www.boost.org/libs/smart_ptr/enable_shared_from_this.html


                typedef T _internal_element_type; // for bcc 5.5.1

                mutable weak_ptr<_internal_element_type> _internal_weak_this;
            };


            結(jié)論

            這個實用類提供了簡單的shared_ptr轉(zhuǎn)換和安全的weak式驗證... 這樣通過繼承就可以使用shared_from_this進(jìn)行安全當(dāng)前類weak_ptr到shared_ptr的轉(zhuǎn)換...

            enable_from_this方法的使用與陷阱
            enable_from_this 的使用與實現(xiàn)原理說明:
            shared_from_this()是enable_shared_from_this的成員函數(shù),返回shared_ptr;
            注意的是,這個函數(shù)僅在shared_ptr的構(gòu)造函數(shù)被調(diào)用之后才能使用。
            原因是enable_shared_from_this::weak_ptr并不在構(gòu)造函數(shù)中設(shè)置,而是在shared_ptr的構(gòu)造函數(shù)中設(shè)置。
            錯誤的使用代碼一:

            #include <boost/shared_ptr.hpp>
            #include <boost/enable_shared_from_this.hpp>

            #include <iostream>
            using namespace std;

            class D: public boost::enable_shared_from_this<D>
            {
            public:
                D()
                {
                    cout<<"D::D()"<<endl;
                    boost::shared_ptr<D> p = shared_from_this();
                }    
            };

            int main()
            {
                boost::shared_ptr<D> a(new D);
                return 0;    
            }


            程序編譯通過,執(zhí)行結(jié)果如下:
            D::D()
            terminate called after throwing an instance of 'boost::exception_detail::clone_impl >'
              what():  tr1::bad_weak_ptr
            Aborted
            說明在D的構(gòu)造函數(shù)中調(diào)用shared_from_this(), 此時D的實例本身尚未構(gòu)造成功,weak_ptr也就尚未設(shè)置,所以程序拋出tr1::bad_weak_ptr異常。
            錯誤的使用代碼二:

            #include <boost/shared_ptr.hpp>
            #include <boost/enable_shared_from_this.hpp>

            #include <iostream>
            using namespace std;

            class D: public boost::enable_shared_from_this<D>
            {
            public:
                D()
                {
                    cout<<"D::D()"<<endl;
                }
                
                void func()
                {
                    cout<<"D::func()"<<endl;
                    boost::shared_ptr<D> p = shared_from_this();
                }    
            };

            int main()
            {
                D d;
                d.func();
                return 0;    
            }


            程序編譯通過,執(zhí)行結(jié)果如下:
            D::D()
            D::func()
            terminate called after throwing an instance of 'boost::exception_detail::clone_impl >'
              what():  tr1::bad_weak_ptr
            Aborted
            失敗原因分析:
            在主函數(shù)main中,D的實例是在棧上構(gòu)造,沒有使用boost::shared_ptr 的構(gòu)造方式,
            所以boost::enable_shared_from_this中的weak_ptr所指的函數(shù)對象也就沒有被賦值,
            在調(diào)用d.func()中使用shared_from_this()函數(shù)時
            ----注:shared_from_this的函數(shù)實現(xiàn) ------
                shared_ptr shared_from_this()
                {
                    shared_ptr p( weak_this_ );
                    BOOST_ASSERT( p.get() == this );
                    return p;
                }
            ----注:shared_from_this的函數(shù)實現(xiàn) ------
            調(diào)用BOOST_ASSERT( p.get() == this );  失敗,拋出以上異常。
            最后,我們給出share_from_this()的正確使用例子:

            #include <boost/shared_ptr.hpp>
            #include <boost/enable_shared_from_this.hpp>

            #include <iostream>
            using namespace std;

            class D: public boost::enable_shared_from_this<D>
            {
            public:
                D()
                {
                    cout<<"D::D()"<<endl;
                }
                
                void func()
                {
                    cout<<"D::func()"<<endl;
                    boost::shared_ptr<D> p = shared_from_this();
                }    
            };

            int main()
            {
                boost::shared_ptr<D> p(new D);
                p->func();
                return 0;    
            }    


            執(zhí)行結(jié)果:
            D::D()
            D::func()

            posted on 2017-03-07 14:17 C++技術(shù)中心 閱讀(1279) 評論(0)  編輯 收藏 引用 所屬分類: C++ 基礎(chǔ)
            久久久久亚洲?V成人无码| 无码国产69精品久久久久网站| 国内精品久久久久| 精品久久人人做人人爽综合| 久久久国产视频| 久久精品国产影库免费看| 国产精品欧美久久久久天天影视| 亚洲精品高清一二区久久| 久久亚洲美女精品国产精品| 久久高清一级毛片| 欧洲人妻丰满av无码久久不卡| 久久99精品国产麻豆不卡| 久久亚洲私人国产精品vA | 老色鬼久久亚洲AV综合| 国产精品欧美久久久久天天影视 | 久久久久久免费视频| 久久精品国产91久久综合麻豆自制| 久久久久婷婷| 国产香蕉97碰碰久久人人| 97精品久久天干天天天按摩| 亚洲中文字幕伊人久久无码 | 欧洲性大片xxxxx久久久| 久久精品国产亚洲av水果派| 久久久久亚洲AV成人网人人网站| 久久人妻少妇嫩草AV蜜桃| 97久久精品无码一区二区| 久久综合噜噜激激的五月天| 奇米影视7777久久精品人人爽| 久久天堂电影网| 日韩欧美亚洲综合久久影院d3| 婷婷综合久久中文字幕蜜桃三电影| 伊人久久大香线蕉综合5g| 老司机午夜网站国内精品久久久久久久久 | 国产69精品久久久久99| 91精品国产91久久| 亚洲一区二区三区日本久久九| 久久久久一区二区三区| 国产精品一区二区久久精品| 国产精品久久久久久久久免费| avtt天堂网久久精品| 国产精品久久久久久久久免费|