在使用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;
}
#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