classimplementation{ Public:~implementation () {std::cout <<"destroying implementation\n"; }voidDo_something () {std::cout <<"Did something\n"; }};voidTest () {boost::shared_ptr<implementation> SP1 (Newimplementation ()); 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";}
Program Output content:
The Sample now has 1 references
The Sample now has 2 references
After Reset SP1. The Sample now has 1 references
Destroying implementation
After Reset SP2.
Summarized as follows:
SHARED_PT can share memory, two shared_ptr directly with the "=" assignment, each shared_ptr inside a counter to record the memory address of how many shared_ptr hold, can be accessed with Use_count (). When the counter is 0 o'clock, delete is automatically called to delete memory.
BOOST Library (1)