您的位置:首页 > 其它

如何把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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: