Most C + +class manages pointer members in one of three ways (1) Regardless of the pointer member. Copy only the pointer, not the object pointed to by the pointer. When one of the pointers releases the space of the object it points to, the other pointers become floating pointers. This is an extreme (2when copied, the pointer is copied, and the object pointed to by the pointer is copied. This can result in wasted space. It is not necessarily necessary to copy the object to which the pointer is pointing. (3The third is a way of compromise. A secondary class is used to manage the replication of pointers. There is a pointer to the auxiliary class in the original class, the data member of the auxiliary class is a counter and a pointer (pointing to the original) (this is how the smart pointer is implemented). In fact, the reference count of smart pointers is similar to the Java garbage collection mechanism: Java's decision to refuse is simple, if an object is not referenced, then the object is garbage. The system can be recycled. #include<iostream>using namespacestd;//defines the U_ptr class that is used only by the Hasptr class to encapsulate usage counts and related pointers//all members of this class are private, and we don't want ordinary users to use the U_ptr class, so it doesn't have any public members//set the Hasptr class to friends so that its members can access the members of U_ptrclassu_ptr{friendclasshasptr; int*IP; size_t use; U_ptr (int*p): IP (P), use (1) {cout<<"U_ptr constructor called!"<<Endl; } ~u_ptr () {DeleteIP; cout<<"u_ptr Distructor called!"<<Endl; }};classhasptr{ Public: //constructor: P is a pointer to an int object that has been created dynamicallyHasptr (int*p): PTR (Newu_ptr (P)) {cout<<"Hasptr constructor called!"<<"Use ="<< Ptr->use <<Endl; } //copy constructor: Copy member and use Count plus 1Hasptr (Consthasptr&orig): ptr (orig.ptr) {++ptr->Use ; cout<<"hasptr copy constructor called!"<<"Use ="<< ptr->use<<Endl; } //assignment operatorhasptr&operator=(Consthasptr&); //destructor: Delete the U_ptr object if the count is 0~hasptr () {cout<<"hasptr Distructor called!"<<"Use ="<< Ptr->use <<Endl; if(--ptr->use = =0) Deleteptr; } //Get data members int*get_ptr ()Const { returnPtr->IP; } //Modifying data members voidSet_ptr (int(PKConst{ptr->ip =p; } //Returns or modifies the underlying int object intGet_ptr_val ()Const { return*ptr->IP; } voidSet_ptr_val (inti) {*ptr->ip =i; }Private: U_ptr*ptr;//point to use the Count class U_ptr}; Hasptr& Hasptr::operator= (ConstHasptr &RHS)//Note that the assignment operator here adds 1 to the use of RHS before reducing the use count of the operands, thus preventing self-assignment{ //increase usage count in right-hand operand++rhs.ptr->Use ; //subtract 1 from the usage count of the left operand object, or delete the object if its usage count is reduced to 0 if(--ptr->use = =0) Deleteptr; PTR= Rhs.ptr;//copy u_ptr pointer return* This;}intMainvoid){ int*pi =New int( the); Hasptr*HPA =NewHasptr (PI);//constructor FunctionHasptr *HPB =NewHasptr (*HPA);//copy ConstructorHasptr *HPC =NewHasptr (*HPB);//copy ConstructorHasptr HPD = *HPA;//copy Constructorcout<< Hpa->get_ptr_val () <<" "<< Hpb->get_ptr_val () <<Endl; HPC->set_ptr_val (10000); cout<< Hpa->get_ptr_val () <<" "<< Hpb->get_ptr_val () <<Endl; Hpd.set_ptr_val (Ten); cout<< Hpa->get_ptr_val () <<" "<< Hpb->get_ptr_val () <<Endl; Deletehpa; DeleteHPB; DeleteHPC; return 0;} Scenario two to avoid each class using pointers in the above scheme to control the reference count itself, a class can be used to encapsulate the pointer. Once encapsulated, this class object can appear anywhere the user class uses pointers, and behaves as a pointer. We can use it like a pointer, without worrying about the problems that ordinary member pointers bring, and we call this class A handle class. When you encapsulate a handle class, you need to request a dynamically allocated reference count space, and the pointer is stored separately from the reference count. Implementation examples such as the following
Design and use of smart pointers in C + +