标签:effective c++ 智能指针 boost shared_ptr
顾名思义,boost::shared_ptr是可以共享所有权的智能指针,首先让我们通过一个例子看看它的基本用法:#include <string> #include <iostream> #include <boost/shared_ptr.hpp> class implementation { public: ~implementation() { std::cout <<"destroying implementation\n"; } void do_something() { std::cout << "did something\n"; } }; void test() { boost::shared_ptr<implementation> sp1(new implementation()); std::cout<<"The Sample now has "<<sp1.use_count()<<" references\n"; boost::shared_ptr<implementation> sp2 = sp1; std::cout<<"The Sample now has "<<sp2.use_count()<<" references\n"; sp1.reset(); std::cout<<"After Reset sp1. The Sample now has "<<sp2.use_count()<<" references\n"; sp2.reset(); std::cout<<"After Reset sp2.\n"; } void main() { test(); }
effective c++条款13-17 “以对象管理资源”之shared_ptr浅析
标签:effective c++ 智能指针 boost shared_ptr
原文地址:http://blog.csdn.net/hustyangju/article/details/41648213