Interface: Http://zh.cppreference.com/w/cpp/memory/shared_ptr This site is better, the concept is relatively concise
How to use: http://www.cnblogs.com/TianFang/archive/2008/09/19/1294521.html
Boost::shared_ptr is not absolutely secure, and the following rules will make it safer for us to use BOOST::SHARED_PTR:
- Avoid direct memory management operations on objects managed by shared_ptr to prevent the object from being re-released
- SHARED_PTR does not automatically manage the memory of objects that are circular references (this is a common problem with various other reference counts for managing memory patterns).
- Do not construct a temporary shared_ptr as a function parameter.
Code instance
#include"stdafx.h"#include<iostream>#include<memory>using namespacestd;classimplementation{ Public: ~implementation () {std::cout <<"destroying implementation\n"; } voidDo_something () {std::cout <<"Did something\n"; }};voidTest () {std::shared_ptr<implementation> SP1 (Newimplementation ()); 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";}intMain () {test ();}
shared_ptr Knowledge Summary