Classes that contain pointers pay particular attention to replication control because the copy pointer copies only the addresses in the pointer, not the object that the pointer points to.
The C + + class manages pointer members in one of 3 ways:
(1) The pointer member takes the normal pointer type behavior. Such a class has all the flaws in the pointer but does not require special replication control.
(2) A class can be an implementation of the "smart pointer" behavior. The object that the pointer points to is shared, but the class prevents the dangling pointer.
(3) class takes the value-type behavior. The object that the pointer points to is unique and is managed separately by each class object.
Here is a summary of the (2) method-The use of a defined smart pointer class
The idea of a smart pointer class is:
In the (1) method, pointers in all objects point directly to the same underlying object (such as an int type object), which results in the destruction of any one of the objects that will
The base object to delete, at which point the pointer member in the other object has become a dangling pointer.
In the case of the (2) method, all objects still have pointer members, but at this point a new count class is added, each of which points to a different base object, but all of this is for the user
Transparent. When I define a pointer in a class object to point to a base object, we do not point the pointer of the class object directly to the underlying object, but instead point to the object's pointer to the defined
Count Class object, the Count class object points to the base class object we were supposed to point to, and the Count class object contains two properties: A pointer to the underlying object, and a counter.
Example
#include <iostream>using namespacestd;classHasptr;//Declaration of the Hasptr class/*U_ptr is a count class that counts the number of Hasptr objects that point to an int object*/classu_ptr{friendclasshasptr; U_ptr (int*p): IP (P), use (1){ }//constructor Function~u_ptr () {delete IP;} int*ip;//instead of pointing directly at an int object through the Hasptr object, you can manage it by U_ptr objectssize_t use;//records How many hasptr objects are pointing to an int object};classhasptr{ Public: Hasptr (int*p,inti)://constructor, the parameter is still the int* type and is constructed by calling the U_ptr constructor .ptrNewu_ptr (P)), Val (i) {} hasptr (ConstHasptr &orig)://copy constructors, at which time PTR and orig.ptr point toPTR (Orig.ptr), Val (orig.val) {++ptr->use;}//The same U_ptr object, and adds 1 to the use of this U_ptr objecthasptr&operator=(Consthasptr&); ~hasptr () {if(--ptr->use==0) Delete ptr; }//Destructors , only when the destructor is//causes the use within the U_ptr object to be reduced to 0 before delete int* GET_PTR ()Const{returnPtr->IP;} intGet_int ()Const{returnVal;} voidSet_ptr (int*P) {ptr->ip=p;} voidSet_int (inti) {val=i;} intGet_ptr_val ()Const{return*ptr->IP;} voidSet_ptr_val (inti) {*ptr->ip=i;}Private: U_ptr*ptr;//now the Hasptr object no longer points directly to the Int object, but to the Count class object intVal;}; Hasptr& Hasptr::operator=(ConstHasptr &obj) { ++obj.ptr->Use ; cout<<"obj.ptr->use="<<obj.ptr->use<<Endl; if(--ptr->use==0) {cout<<"ptr->use="<<ptr->use<<Endl; Delete ptr; } ptr=obj.ptr; Val=Obj.val; return* This;}intMain () {intnum1=Ten; intNum2= One; Hasptr Obj1 (&NUM1, -); Hasptr Obj2 (&NUM2, A); cout<<"obj1:"<<Endl; cout<<"obj1.get_int ():"<<obj1.get_int () <<Endl; cout<<"obj1.get_ptr_val ():"<<obj1.get_ptr_val () <<endl<<Endl; cout<<"Obj2:"<<Endl; cout<<"obj2.get_int ():"<<obj2.get_int () <<Endl; cout<<"obj2.get_ptr_val ():"<<obj2.get_ptr_val () <<endl<<Endl; Obj1=Obj2; cout<<endl<<"obj1:"<<Endl; cout<<"obj1.get_int ():"<<obj1.get_int () <<Endl; cout<<"obj1.get_ptr_val ():"<<obj1.get_ptr_val () <<endl<<Endl; return 0;}
Results after the compilation run:
obj1:obj1.get_int (): -obj1.get_ptr_val ():Tenobj2:obj2.get_int (): Aobj2.get_ptr_val (): Oneobj.ptr->use=2ptr->use=0obj1:obj1.get_int (): Aobj1.get_ptr_val (): OneProcess returned0(0x0) Execution Time:2.007spress any key toContinue.
C + + Self-study Note _ Define Smart pointer class _ C + + Primer