share_ptr使用
#include <boost/smart_ptr.hpp>
#include <iostream>
using namespace std;
class Configure
{
public:
int num = 0;
};
const boost::shared_ptr<Configure> DEFAULT_CONFIGURE(new Configure());
class ItemInfo
{
public:
boost::shared_ptr<const Configure> _configure;
public:
ItemInfo(boost::shared_ptr<const Configure> configure = DEFAULT_CONFIGURE){ _configure = configure;}
void set_configure(const boost::shared_ptr<Configure> configure) {_configure = configure;}
void print() {cout << _configure.get()->num << "\t" << _configure.use_count() << endl;}
};
int main(int argc, char* argv[])
{
boost::shared_ptr<Configure> configure(new Configure());
configure.get()->num = 10;
ItemInfo info;
info.set_configure(configure);
info.print();
info._configure.get()->num = 20;
return 0;
}
g++ -std=c++11 test_shared_ptr.cpp -o test -I /home/users/titans/third-party/boost/include -L /home/users/titans/third-party/boost/lib -lboost_system -lboost_filesystem
浙公网安备 33010602011771号