shared_ptr Knowledge Summary

Source: Internet
Author: User

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:

      1. Avoid direct memory management operations on objects managed by shared_ptr to prevent the object from being re-released
      2. 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).
      3. Do not construct a temporary shared_ptr as a function parameter.
    Code instance
    1. #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

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.