BOOST Library (1)

Source: Internet
Author: User

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)

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.