Object-Oriented Programming-c++_ class 26 copy construction ⅰ_ 27 copy Construction Ⅱ

Source: Internet
Author: User

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 Ⅱ

Related Article

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.