Smart pointers for C + + (2)

Source: Internet
Author: User

weak_ptr Example

/*The main reason for using shred_ptr is to avoid focusing on the resource that the pointer is pointing to//only the pointer will automatically release related resources to objects that are no longer needed//But in some cases, this is not what we need. For example, a circular reference. Two objects all refer to each other. or share an object but do not occupy the object//This time can use weak_ptr in fact relative shared_ptr the pointer does not increase the count//*/#include<iostream>#include<string>#include<vector>#include<memory>using namespacestd;classperson{ Public:    stringname; shared_ptr<Person>mother; shared_ptr<Person>father; Vector<shared_ptr<Person>>Kids; Person (Const string&N, shared_ptr<Person> m =nullptr, shared_ptr<Person> f =nullptr): Name (n), mother (M), Father (f) {}~Person () {cout<<"Delete"<< name <<Endl; }};shared_ptr<Person> initfamily (Const string&name) {shared_ptr<Person> Mom (NewPerson (name +"' s mom")); shared_ptr<Person> Dad (NewPerson (name +"' s dad")); shared_ptr<Person> Kid (NewPerson (Name,mom,dad)); Mom-Kids.push_back (Kid); Dad-Kids.push_back (Kid); returnKid;}int_tmain (intARGC, _tchar*argv[]) {shared_ptr<Person> p = initfamily ("Nico"); cout<<"Nico ' s family exists"<<Endl; cout<<"-Nico is shared"<< P.use_count () <<" Times"<<Endl; cout<<"-Name of 1st Kid of Nico ' s mom:"<< p->mother->kids[0]->name <<Endl; P= Initfamily ("Jim"); cout<<"Jim ' s family exists"<<Endl;    return 0;}

The result of running the code is as follows:

Nico ' s family exists
-Nico is shared 3 times
-Name of 1st Kid of Nico ' s Mom:nico
Jim ' s family exists
Please press any key to continue ...

We don't actually see the pointer pointing to the release of the resource because the shared_ptr pointer is used in the vector in class person to increase the count of resources, so it's not released in time.

We should change the vector to weak_ptr and use the lock () function to promote the weak_ptr to shared_ptr.

/*The main reason for using shred_ptr is to avoid focusing on the resource that the pointer is pointing to//only the pointer will automatically release related resources to objects that are no longer needed//But in some cases, this is not what we need. For example, a circular reference. Two objects all refer to each other. or share an object but do not occupy the object//This time can use weak_ptr in fact relative shared_ptr the pointer does not increase the count//*/#include<iostream>#include<string>#include<vector>#include<memory>using namespacestd;classPerson { Public:    stringname; shared_ptr<Person>mother; shared_ptr<Person>father; Vector<weak_ptr<Person>> Kids;//weakpointer!!!Person (Const string&N, shared_ptr<Person> m =nullptr, shared_ptr<Person> f =nullptr): Name (n), mother (M), Father (f) {}~Person () {cout<<"Delete"<< name <<Endl; }};shared_ptr<Person> initfamily (Const string&name) {shared_ptr<Person> Mom (NewPerson (name +"' s mom")); shared_ptr<Person> Dad (NewPerson (name +"' s dad")); shared_ptr<Person> Kid (NewPerson (Name,mom,dad)); Mom-Kids.push_back (Kid); Dad-Kids.push_back (Kid); returnKid;}int_tmain (intARGC, _tchar*argv[]) {shared_ptr<Person> p = initfamily ("Nico"); cout<<"Nico ' s family exists"<<Endl; cout<<"-Nico is shared"<< P.use_count () <<" Times"<<Endl; cout<<"-Name of 1st Kid of Nico ' s mom:"<< p->mother->kids[0].Lock()->name <<Endl; P= Initfamily ("Jim"); cout<<"Jim ' s family exists"<<Endl; return 0;}

The operation results are as follows

Nico ' s family exists
-Nico is shared 1 times
-Name of 1st Kid of Nico ' s Mom:nico
Delete Nico
Delete Nico ' s dad
Delete Nico ' s mom
Jim ' s family exists
Delete Jim
Delete Jim ' s dad
Delete Jim ' s mom
Please press any key to continue ...

You can see that the resource is released correctly

Code adapted from C + + standard library-Self-study tutorial and Reference Manual English second Edition

Smart pointers for C + + (2)

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.