Before starting this article, let's summarize the previous content:
1. The smart pointer uses the RAII mechanism to initialize the resources when the object is constructed, and to clean up and Shanwei the resources when the objects are reconstructed.
2.auto_ptr prevents the copy from being refactored to release the same piece of memory, using the "Transfer ownership" method. (actual development of auto_ptr is not practical)
3.scoped_ptr is similar to auto_ptr, but the biggest difference between it and auto_ptr is that it cannot transfer ownership, that is, prohibit copy/assignment! (Of course, we also explored the technique of banning copying objects in C + +.)
After reviewing the contents of the previous article, we will discuss shared_ptr today.
Although we have scoped_ptr, in the actual development process, we really want to copy the smart pointer, then scoped_ptr beyond.
So, let's go back to the original question: What happens when you copy a smart pointer?
We also analyzed in the second article: If the smart pointer is not special processing, when the destruction of the same memory will be released multiple times, the program will crash!
Therefore, if we want to copy the smart pointer, we have to do some special processing, so that the destructor only frees the memory once.
At this point, if you have explored the depth of the copy of the students, may have the heart of the answer: With reference count!!! (Deep copy problem, I will discuss later, not the focus of this article)
Considering that some children's shoes may not know what a reference count is, I'll explain here:
In the reference count, each object is responsible for maintaining the count value of all references to the object. When a new reference is directed to an object, the reference counter increments, and when a reference is removed, the reference count is decremented. When the reference count is 0 o'clock, the object frees the owning resource. --Baidu Encyclopedia
A bit more popular: In this example, we use the count variable to record how many objects are currently working together to maintain the pointer, and each time you copy/assign a value, let count++.
Copy construction (similar to assignment operator overloading, call copy construction in concise notation) sharedptr (const sharedptr &SP) : _ptr (sp._ptr) , _count (sp._count) { if (_count!=null) { + + (*_count); }}
When the object is refactored, we first look at count is not 1, if not 1, there are other objects in the maintenance of this pointer, we let count--. otherwise, it means that only the current object in the maintenance of this pointer, at this point can happily delete the pointer.
~sharedptr () { if (count!=null && *count = = 1) { delete _ptr; Delete _count; _ptr = NULL; _count = NULL; } else{ -(*count); }}
In this form, it is guaranteed that a single piece of memory can be maintained in multiple shared_ptr objects, with only one delete for memory.
Finally, post my simplified shared_ptr code.
/** File Description: Simulation implementation shared_ptr* Author: High Minor * Date: 2017-03-31* integrated development environment: Microsoft Visual Studio */#pragma oncetemplate<typename T>class sharedptr{public://constructor sharedptr (T *ptr=null): _ptr (PTR), _count ( NULL) {if (ptr!=null) {_count = new int (1);std::cout<<_ptr<< "is Created" <<STD::ENDL;}} Copy construction sharedptr (const SHAREDPTR & SP): _ptr (Sp._ptr), _count (Sp._count) {if (sp._count!=null) {+ + (*_count);}} The assignment operator Overloads sharedptr& operator= (const sharedptr &SP) {if (_ptr!=sp._ptr) {sharedptr tmp (SP); Std::swap (_ptr,tmp. _PTR); Std::swap (_count,tmp._count);} return *this;} destructor ~sharedptr () {if (_count!=null &&-(*_count) ==0) {std::cout<<_ptr<< "is Destory" << Std::endl;delete _ptr;delete _count;_ptr = Null;_count = NULL;}} private:t* _ptr;int *_count;}; void Testsharedptr () {sharedptr<int> SP1 (new int (10)); Sharedptr<int> SP2 (new int (20)); sharedptr<int> SP3 (new int (30)); Sharedptr<int> SP4 (SP1); SP2 = SP1;SP3 = SP1;}
C + + smart pointers in detail (iv): shared_ptr