For tr1: shared_ptr, VS is installed on its own, but earlier versions do not exist. As the implementation product of tr1, boost includes
"Algorithms
Broken compiler workarounds
Concurrent Programming
Containers
Correctness and Testing
Data Structures
Domain Specific
Function objects and higher-order Programming
Generic programming
Image Processing
Input/Output
Inter-language support
Iterators
Language Features emulation
Math and numerics
Memory
Parsing
Patterns and idioms
Preprocessor metaprogramming
Programming interfaces
State machines
String and Text Processing
System
Template metaprogramming
Miscellaneous"
And a series of more secure and rich implementations of C ++ functions or libraries. You can download it from the official website, which contains detailed API documentation and routines. Boost: shared_ptr is the implementation of shared_ptr of tr1. The usage of smart pointers (class pointer objects) is similar. For details, see the code below.
Header file
# Include <iostream> using namespace STD; Class test {public: Test ();~ Test ();};
Implementation File
# Include "head. H" test: Test () {cout <"construct test." <Endl;} test ::~ Test () {cout <"destruct test." <Endl ;}
Main File
// Shared_ptr header file # include <boost/shared_ptr.hpp> in boost // The libraries installed in vs2005 do not contain shared_ptr # include <memory> # include "head. H "// experiment tr1: shared_ptrvoid test_shared_ptr () {cout <" I am tr1. "<Endl; tr1 :: shared_ptr <Test> p_test (new test ();} // experiment boost/shared_ptrvoid test_boost_shared_ptr () {cout <"I am boost. "<Endl; Boost: shared_ptr <Test> p_boost_test (new test ();} int main () {test_shared_ptr (); cout <"*************************" <Endl; test_boost_shared_ptr (); system ("pause ");}
Execution result:
The above two methods are the best implementation of object management resources. The standard library also provides auto_ptr, but such objects cannot be copied.
For the idea of using objects to manage resources, the principle of C ++ T Recommendation is raiI, that is, resource aquisition is initialasition.
**************************************** ***********************************
Original, link: http://blog.csdn.net/u012150179/article/details/37965931
Smart pointer tr1: shared_ptr, boost: shared_ptr usage