C++ 技术中心

   :: 首页 :: 联系 ::  :: 管理
  160 Posts :: 0 Stories :: 87 Comments :: 0 Trackbacks

公告

郑重声明:本BLOG所发表的原创文章,作者保留一切权利。必须经过作者本人同意后方可转载,并注名作者(天空)和出处(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.
继承该类就可以进行基于当前子类进行安全的weap_ptr到shared_ptr的转换...

代码实例
以下代码中Y类继承enable_shared_from_this,, 从而我们可以直接在函数中调用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);
// 调用f获得shared_ptr

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

}


该类的实现


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;
};


结论

这个实用类提供了简单的shared_ptr转换和安全的weak式验证... 这样通过继承就可以使用shared_from_this进行安全当前类weak_ptr到shared_ptr的转换...

enable_from_this方法的使用与陷阱
enable_from_this 的使用与实现原理说明:
shared_from_this()是enable_shared_from_this的成员函数,返回shared_ptr;
注意的是,这个函数仅在shared_ptr的构造函数被调用之后才能使用。
原因是enable_shared_from_this::weak_ptr并不在构造函数中设置,而是在shared_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;
        boost::shared_ptr<D> p = shared_from_this();
    }    
};

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


程序编译通过,执行结果如下:
D::D()
terminate called after throwing an instance of 'boost::exception_detail::clone_impl >'
  what():  tr1::bad_weak_ptr
Aborted
说明在D的构造函数中调用shared_from_this(), 此时D的实例本身尚未构造成功,weak_ptr也就尚未设置,所以程序抛出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;    
}


程序编译通过,执行结果如下:
D::D()
D::func()
terminate called after throwing an instance of 'boost::exception_detail::clone_impl >'
  what():  tr1::bad_weak_ptr
Aborted
失败原因分析:
在主函数main中,D的实例是在栈上构造,没有使用boost::shared_ptr 的构造方式,
所以boost::enable_shared_from_this中的weak_ptr所指的函数对象也就没有被赋值,
在调用d.func()中使用shared_from_this()函数时
----注:shared_from_this的函数实现 ------
    shared_ptr shared_from_this()
    {
        shared_ptr p( weak_this_ );
        BOOST_ASSERT( p.get() == this );
        return p;
    }
----注:shared_from_this的函数实现 ------
调用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;    
}    


执行结果:
D::D()
D::func()

posted on 2017-03-07 14:17 C++技术中心 阅读(1223) 评论(0)  编辑 收藏 引用 所属分类: C++ 基础

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理