Copy constructor, not byte-to-byte copy, but member-to-member copy
constructor, a parameter is a reference to a class
That's right
1#include <iostream>2#include <string>3 using namespacestd;4 5 Static intObjectcount =0;6 7 classHowmany8 {9 Public:TenHowmany () {objectcount++; print ("Howmany ()"); }//constructor, no arguments OneHowmany (inti) {objectcount++; print ("howmany (int)"); }//constructor, the argument is an int AHowmany (Consthowmany& o) {objectcount++; print ("Howmany (HM)"); }//constructor, a parameter is a reference to a class - voidPrintConst string& msg ="") - { the if(Msg.size ()! =0) - { -Std::cout << msg <<": "; - } +Std::cout <<"objectcount=" -<< Objectcount <<Std::endl; + } A~Howmany () at { -objectcount--; -Print"~howmany ()"); - } - }; - in Howmany F (howmany x) - { toStd::cout <<"begin of F"<<Std::endl; +X.print ("x argument inside F ()"); -Std::cout <<"end of F"<<Std::endl; the returnx; * } $ Panax Notoginseng voidMain () - { the Howmany H; +H.print ("After construction of H"); A theHowmany H2 =f (h); + //howmany h2 = h; - //Howmany H2 (ten);//constructor, the argument is an int $ //howmany H2 (h);//constructor, parameter is a class $ -H.print ("After call to F ()"); - theSystem"Pause"); -}
constructor, parameter is a new class that forms a dead loop
Error
1>main.cpp: Error C2652: "Howmany": illegal copy constructor: The first argument should not be "howmany"
1> main.cpp (8): note: see Declaration of "Howmany"
1>main.cpp: Error C2333: "Howmany::howmany": There are errors in the function declaration; Skip function Body
1>main.cpp: Error C2440: "Return": Cannot convert from "Howmany" to "Howmany"
1> main.cpp: Note: The construction class "Howmany" cannot be copied because the copy constructor is ambiguous or no copy constructor is available
1>main.cpp: Error C2664: "Howmany F (howmany)": cannot convert parameter 1 from "Howmany" to "Howmany"
1> main.cpp: Note: The construction class "Howmany" cannot be copied because the copy constructor is ambiguous or no copy constructor is available
1#include <iostream>2#include <string>3 using namespacestd;4 5 Static intObjectcount =0;6 7 classHowmany8 {9 Public:TenHowmany () {objectcount++; print ("Howmany ()"); }//constructor, no arguments OneHowmany (inti) {objectcount++; print ("howmany (int)"); }//constructor, the argument is an int A -Howmany (Howmany o) {objectcount++; print ("Howmany (HM)"); }//constructor, error, parameter is a new class that forms a dead loop - the //1>main.cpp: Error C2652: "Howmany": illegal copy constructor: The first argument should not be "Howmany" - //1> main.cpp (8): note: see Declaration of "Howmany" - //1>main.cpp: Error C2333: "Howmany::howmany": There are errors in the function declaration; Skip function Body - //1>main.cpp: Error C2440: "Return": Cannot convert from "Howmany" to "Howmany" + //1> main.cpp: Note: The construction class "Howmany" cannot be copied because the copy constructor is ambiguous or no copy constructor is available - //1>main.cpp: Error C2664: "Howmany F (howmany)": cannot convert parameter 1 from "Howmany" to "Howmany " + //1> main.cpp: Note: The construction class "Howmany" cannot be copied because the copy constructor is ambiguous or no copy constructor is available A at voidPrintConst string& msg ="") - { - if(Msg.size ()! =0) - { -Std::cout << msg <<": "; - } inStd::cout <<"objectcount=" -<< Objectcount <<Std::endl; to } +~Howmany () - { theobjectcount--; *Print"~howmany ()"); $ }Panax Notoginseng }; - the Howmany F (howmany x) + { AStd::cout <<"begin of F"<<Std::endl; theX.print ("x argument inside F ()"); +Std::cout <<"end of F"<<Std::endl; - returnx; $ } $ - voidMain () - { the Howmany H; -H.print ("After construction of H");Wuyi theHowmany H2 =f (h); - //howmany h2 = h; Wu //Howmany H2 (ten);//constructor, the argument is an int - //howmany H2 (h);//constructor, parameter is a class About $H.print ("After call to F ()"); - -System"Pause"); -}
Object-Oriented Programming-c++_ class 26 copy construction ⅰ_ 27 copy Construction Ⅱ