Smart pointer: A common implementation method is to reference the counting method. A smart pointer associates a counter with the object to which the class points. The reference count trail shares the same pointer with a total of class objects.
Each time a new object of the class is created, the pointer is initialized and the reference count is set to 1. when an object is created as a copy of another object, copy the constructor to copy the pointer and add the corresponding reference count. when an object is assigned a value, the value assignment operator reduces the reference count of the object indicated by the left operand (if the reference count is reduced to 0, delete the object), and add the reference count of the object indicated by the right operand. This is the object pointed by the Left pointer to the right pointer, therefore, the reference count of the object pointed to by the right pointer is + 1. When the Destructor is called, the constructor reduces the reference count (if the reference count is reduced to 0, the base object is deleted ). There are two classic strategies for implementing smart pointers: one is to introduce helper classes and the other is to use handle classes. Here we will mainly introduce the methods of helper classes. Let's look at the example below:
Class Point // basic object class. Make a smart pointer to the Point class {public: Point (INT xval = 0, int yval = 0): X (xval ), Y (yval) {} int getx () const {return X;} int Gety () const {return y;} void setx (INT xval) {x = xval ;} void sety (INT yval) {Y = yval;} PRIVATE: int X, Y ;}; class refptr // helper class {// All Access Permissions of such members are private, because you do not want to directly use the friend class smartptr; // you do not need to define the smart pointer class as youyuan, because the smart pointer class needs to directly manipulate the auxiliary class refptr (point * PTR): p (PTR ), count (1 ){}~ Refptr () {Delete P;} int count; // reference count point * P; // Base Object Pointer}; Class smartptr // smart pointer class {public: smartptr (point * PTR): RP (New refptr (PTR) {}// constructor smartptr (const smartptr & SP): RP (sp. RP) {++ RP-> count;} // copy the constructor smartptr & operator = (const smartptr & RHs) {// overload value assignment operator + + RHS. RP-> count; // first add 1 to the reference count of the right operand, if (-- RP-> COUNT = 0) // then subtract 1 from the reference count, can handle the auto-assigned Delete RP; RP = RHS. RP; return * This ;}~ Smartptr () {// destructor if (-- RP-> COUNT = 0) // when the reference count is reduced to 0, delete the helper class object pointer, delete the basic object Delete RP;} PRIVATE: refptr * RP; // helper class Object Pointer}; int main () {point * P1 = new point (10, 8 ); smartptr SP1 (P1); // in this case, sp1.rp-> COUNT = 1 smartptr SP2 (SP1); // first, assign sp1.rp-> count to sp2.rp-> count, then sp2.rp-> count ++. In SP1, the RP of SP2 is the same object point * P2 = new point (5, 5); smartptr SP3 (P2); SP3 = SP1; return 0 ;}
The memory structure using this method is as follows: