enable_from_this方法的使用与陷阱
来源:互联网 发布:c语言无限循环代码 编辑:程序博客网 时间:2024/05/20 05:04
enable_from_this 的使用与实现原理说明:
shared_from_this()是enable_shared_from_this<T>的成员函数,返回shared_ptr<T>;
注意的是,这个函数仅在shared_ptr<T>的构造函数被调用之后才能使用。
原因是enable_shared_from_this::weak_ptr并不在构造函数中设置,而是在shared_ptr<T>的构造函数中设置。
错误的使用代码一:
#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<D> 的构造方式,
所以boost::enable_shared_from_this<D>中的weak_ptr所指的函数对象也就没有被赋值,
在调用d.func()中使用shared_from_this()函数时
----注:shared_from_this的函数实现 ------
shared_ptr<T> shared_from_this()
{
shared_ptr<T> 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_from_this方法的使用与陷阱
- enable_from_this方法的使用与陷阱
- exec与spawn方法的区别与陷阱
- Java 中 List.subList() 方法的使用陷阱
- 使用Runtime.getRuntime().exec()方法的几个陷阱 (转)
- (转)Linux共享内存使用常见陷阱与分析(4)-共享内存删除的陷阱
- StringBuffer的使用陷阱
- QWT的使用陷阱
- daemon 的使用陷阱
- 使用nuget的陷阱
- SwipeBackLayout的使用陷阱
- goroutine的使用陷阱
- goroutine的使用陷阱
- static与final使用陷阱
- Mock使用的优势与陷阱(文章收集)
- C/C++指针形参的使用与陷阱
- getparent方法的一些陷阱
- Thread.interrupted()方法的陷阱
- JAVA文本输入输出流的几种方法比较
- Redis高可用性研究和选型
- div+css 下拉多级弹出菜单
- MFC框架类、文档类、视图类相互访问的方法(转载)
- 0004 java中volatile关键字的含义【入门】
- enable_from_this方法的使用与陷阱
- (Relax ST1.12)POJ 2606 Rabbit hunt(给出若干个点,求最多能有多少个点共线)
- ie中有双倍margin的问题
- jQuery Ajax 全解析
- Codeforces 348A - Mafia (二分答案)
- 使用$.getJSON实现跨域ajax请求
- 如何在一个设备上安装一个App的两个不同版本
- s3c2416解决无法下载大于60M文件系统
- mysql 批量删除索引外键