标签:
接口:http://zh.cppreference.com/w/cpp/memory/shared_ptr 这个网站比较好,概念比较简洁
如何使用:http://www.cnblogs.com/TianFang/archive/2008/09/19/1294521.html
boost::shared_ptr并不是绝对安全,下面几条规则能使我们更加安全的使用boost::shared_ptr:
#include "stdafx.h" #include <iostream> #include <memory> using namespace std; class implementation { public: ~implementation() { std::cout << "destroying implementation\n"; } void do_something() { std::cout << "did something\n"; } }; void test() { std::shared_ptr<implementation> sp1(new implementation()); std::cout << "The Sample now has " << sp1.use_count() << " references\n"; std::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"; } int main() { test(); }
标签:
原文地址:http://www.cnblogs.com/hellozhuzi/p/5720921.html