We all know that when two pointers direct to the same variable, if one pointer is released, the other will have a problem. To illustrate the problem, I made a very disgusting small example to copy the Code class C {public: C (int v) {ptrInt = new int; * ptrInt = v; valueInt = v ;}~ C () {} void DelIntV () {valueInt = 0; delete ptrInt;} C (const C & c) {} int * ptrInt; int valueInt; private :}; int main () {C c1 (2); C c2 (3); c2 = c1; std :: cout <"ptrInt" <c2.ptrInt <"value" <* c2.ptrInt <std: endl; std :: cout <"valueInt" <c2.valueInt <std: endl; c1.DelIntV (); std :: cout <"address" <c2.ptrInt <"value" <* c2.ptrInt <std: endl; std :: cout <"valueInt" <c2.valueInt <std: endl; std: cin. Get (); return 0;} copy the Code. This is to assign c1 to c2, then output the ptrInt value and valueInt value of the pointer, and then assign the c1 pointer to delete, if the value of valueInt is 0 and the ptrInt and valueInt values of c2 are output, the pointer is faulty. Check the output result. To solve this problem, I first thought of the overload operator = copying code C & operator = (const C & c) {if (this! = & C) {delete ptrInt; ptrInt = new int; * ptrInt = * c. ptrInt; valueInt = c. valueInt;} return * this;} copy complete code copy Code class C {public: C (int v) {ptrInt = new int; * ptrInt = v; valueInt = v ;} ~ C () {} void DelIntV () {valueInt = 0; delete ptrInt;} C (const C & c) {} int * ptrInt; int valueInt; C & operator = (const C & c) {if (this! = & C) {delete ptrInt; ptrInt = new int; * ptrInt = * c. ptrInt; valueInt = c. valueInt;} return * this;} private:}; int main () {C c1 (2); C c2 (3); c2 = c1; std :: cout <"ptrInt" <c2.ptrInt <"value" <* c2.ptrInt <std: endl; std :: cout <"valueInt" <c2.valueInt <std: endl; c1.DelIntV (); std :: cout <"address" <c2.ptrInt <"value" <* c2.ptrInt <std: endl; std :: cout <"valueInt" <c2.valueInt <std: endl; std: cin. Get (); return 0;} copy the code and check the output result again: this is correct, but if we make a modification in the main function, copy the code int main () {C c1 (2); C c2 = c1; // The value std :: cout <"ptrInt" <c2.ptrInt <"value" <* c2.ptrInt <std: endl; std :: cout <"valueInt" <c2.valueInt <std: endl; c1.DelIntV (); std :: cout <"address" <c2.ptrInt <"value" <* c2.ptrInt <std: endl; std :: cout <"valueInt" <c2.valueInt <std: endl; std: cin. get (); return 0;} After copying the code, the error will be the same as before. Why? compile In Class c, we will find a copy constructor. If it cannot be found, we will create a copy constructor by ourselves, even if we reload operator =, the replica constructor created by the compiler will still assign c1 to c2 in the "copy by users" mode, so we need to re-implement this replica const, className (const className & cn ); I am doing this in C (const C & c) {* this = c;} Here = is actually calling the heavy load = complete method code copy Code class C {public: C (int v) {ptrInt = new int; * ptrInt = v; valueInt = v ;}~ C () {} void DelIntV () {valueInt = 0; delete ptrInt;} C (const C & c) {* this = c;} int * ptrInt; int valueInt; C & operator = (const C & c) {if (this! = & C) {delete ptrInt; ptrInt = new int; * ptrInt = * c. ptrInt; valueInt = c. valueInt;} return * this;} private:}; int main () {C c1 (2); C c2 = c1; // std :: cout <"ptrInt" <c2.ptrInt <"value" <* c2.ptrInt <std: endl; std :: cout <"valueInt" <c2.valueInt <std: endl; c1.DelIntV (); std :: cout <"address" <c2.ptrInt <"value" <* c2.ptrInt <std: endl; std :: cout <"valueInt" <c2.valueInt <std: endl; std: cin. get (); return 0;