Smart pointer tr1: shared_ptr, boost: shared_ptr usage

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.