ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

(转)enable_from_this方法的使用与陷阱

2019-03-17 18:48:55  阅读:305  来源: 互联网

标签:enable boost weak 陷阱 shared include 方法 ptr


转自http://blog.chinaunix.net/uid-442138-id-2122464.html   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<boost::exception_detail::error_info_injector<boost::bad_weak_ptr> >'   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<boost::exception_detail::error_info_injector<boost::bad_weak_ptr> >'   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()

标签:enable,boost,weak,陷阱,shared,include,方法,ptr
来源: https://www.cnblogs.com/wuwangchuxin0924/p/10548216.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有