C ++ replica Constructor

Source: Internet
Author: User

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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.