如何把this指针转换成boost的shared_ptr
2011-10-10 10:58
267 查看
在使用shared_ptr的时候碰到了这个问题,直接上代码示例,编译: g++ shared_ptr.cpp -I ~/usr_spider/include/ -L ~/usr_spider/lib64
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <iostream>
using namespace std;
class test: public boost::enable_shared_from_this<test> {
public:
int num;
boost::shared_ptr<test> get_sharedPtr_from_this(){
return shared_from_this();
}
};
typedef boost::shared_ptr<test> testPtr;
main(){
testPtr a(new test);
a->num = 33;
testPtr b = a->get_sharedPtr_from_this();
cout<<b->num<<endl;
}
参考:http://www.boost.org/doc/libs/1_47_0/libs/smart_ptr/enable_shared_from_this.html
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <iostream>
using namespace std;
class test: public boost::enable_shared_from_this<test> {
public:
int num;
boost::shared_ptr<test> get_sharedPtr_from_this(){
return shared_from_this();
}
};
typedef boost::shared_ptr<test> testPtr;
main(){
testPtr a(new test);
a->num = 33;
testPtr b = a->get_sharedPtr_from_this();
cout<<b->num<<endl;
}
参考:http://www.boost.org/doc/libs/1_47_0/libs/smart_ptr/enable_shared_from_this.html
相关文章推荐
- 如何把this指针转换成boost的shared_ptr
- 【C++/Boost Lib】如何初始化一个shared_ptr指针。
- 详解 boost 库智能指针(scoped_ptr<T> 、shared_ptr<T> 、weak_ptr<T> 源码分析)
- boost::shared_ptr智能指针分析
- 智能指针tr1::shared_ptr、boost::shared_ptr使用
- [6]智能指针boost::shared_ptr
- boost::shared_ptr shared_from_this
- 关于boost 库 shared_ptr 智能指针的循环引用【2013.10.22】
- Boost智能指针——scoped_ptr和shared_ptr
- 【Boost】系列03:内存管理之shared_ptr智能指针
- Boost智能指针——shared_ptr
- 智能指针tr1::shared_ptr、boost::shared_ptr使用
- 浅析boost之智能指针—shared_ptr
- 多索引容器boost::multi_index_container储存共享智能指针boost::shared_ptr
- boost 智能指针auto_ptr scoped_ptr shared_ptr scoped_array shared_array 总结
- boost::weak_ptr和enable_shared_from_this
- Boost智能指针——shared_ptr
- 从零开始学C++之boost库(一):详解 boost 库智能指针(scoped_ptr<T> 、shared_ptr<T> 、weak_ptr<T> 源码分析)
- Boost智能指针——shared_ptr
- boost::shared_ptr 智能指针在项目中的使用