"C + +" smart pointer Brief (vi): Smart pointer Summary and supplement

Source: Internet
Author: User

In this paper, we mainly summarize the relevant principles and implementation of the smart pointers introduced earlier, and by the way, add the contents of the shared_ptr that are not mentioned in the previous article.

Summarize:

1. The smart pointer, through the RAII mechanism, constructs the object when the resource is initialized, and when the object is destructor, the resource is cleaned and Shanwei.

2.auto_ptr, prevents the copy/assignment of objects, the destruction of multiple delete objects causes the program to crash, so it through "transfer ownership", the completion of assignment/copy, to ensure that only one object maintenance, release pointers. It is not commonly used in real development.

3.scoped_ptr is similar to auto_ptr, except that it does not "transfer ownership", but prohibits the copying/assignment of objects. (The copy constructor, assignment operator overload is set to private, and only the declaration is given, no definition is provided)

4.shared_ptr, which manages pointers by reference counting, increases the reference count when a new object maintains the same pointer. The delete pointer is only true when the reference count is 1 o'clock.

5.weak_ptr, to solve the problem of shared_ptr circular reference, used with shared_ptr, cannot be used alone.

Add: shared_ptr Custom Remove

From the previous article, we already know that shared_ptr is a more practical smart pointer.

We can manage pointers with very concise statements:

#include <iostream> #include <memory>using namespace Std;int main () {shared_ptr<int> SP1 (new int (10)) ; cout<<*sp1<<endl;*sp1 = 20;cout<<*sp1<<endl;        return 0;}

For the memory that we dynamically request, the construction time is initialized, and the destructor is freed through delete without any problems.

But what if it's a pointer to a file type?

#include <iostream> #include <memory>using namespace Std;int main () {shared_ptr<file> SP1 (fopen (" Test.txt "," w "));//If no special treatment is performed, the program crashes!return 0;}

As a result, shared_ptr provides the function of the undelete, which we can use to recycle specific resources through custom-made deletes.

#include <iostream> #include <memory>using namespace std;//faux function struct fclose{void operator (void *ptr) { Fclose ((file*) ptr);cout<< "Don't worry, shared_ptr has closed the file by calling me!" <<endl;}}; int main () {///We complete the cleanup of the file pointer by passing in the functor Shared_ptr<file> SP1 (fopen ("Test.txt", "W"), Fclose ()); return 0;}

At this point, the custom shared_ptr removal section is here to complete the finale!

So, the smart pointer part here is over .... Thanks for reading!

"C + +" smart pointer Brief (vi): Smart pointer Summary and supplement

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.