C + + Knowledge Point: Copy constructor example

Source: Internet
Author: User

//copy constructor://a copy constructor is called once when a function parameter is passed, and a copy constructor is called once when the object is assigned a value, and the object is passed as a parameter and is destroyed in time. #include <fstream>#include<string>using namespaceStd;ofstream out("Howmany2.out");classhowmany2{stringName//Object identifier    Static intObjectcount; Public: HowMany2 (Const string& id =""): Name (ID) {++Objectcount; Print ("HowMany2 ()"); }    ~HowMany2 () {--Objectcount; Print ("~howmany2 ()"); }    //The copy-constructor;HowMany2 (Consthowmany2&h): Name (h.name) {name+="Copy"; ++Objectcount; Print ("HowMany2 (const howmany2&)"); }    voidPrintConst string& msg ="")Const    {        if(Msg.size ()! =0)        {             out<< msg <<Endl; }         out<<'\ t'<< name <<":"<<"objectcount="<< Objectcount <<Endl; }};intHowmany2::objectcount =0;//Pass and return by value:HowMany2 F (HowMany2 x) {X.print ("x argument inside F ()");  out<<"returning from F ()"<<Endl; returnx;}intMain () {HowMany2 h ("h"); //1.howmany2 ()//2.h:objectcount = 1     out<<"Entering F ()"<<Endl; //3.Entering F ()    /*enter F (h) At this point the copy constructor is called by the compiler to complete the pass-through process, creating a new object within F (), which is a copy of H, so the object becomes 2 output://The copy construction occurs when the object is passed as a function parameter 4.howmany2 (const HO wmany2&) 5.h copy:objectcount=2 6.x argument inside F () 7.h copy:objectcount=3 8.returning from F () 8th    Shows the beginning of the return from F (), but before the local variable "H-Copy" is destroyed (the local variable is scoped at the end of the function) he must be copied into the return value, which is H2.    Objects that were not previously created H2 are created from the current object (local variables within the function f ()) so the copy constructor is called in the 9th row.    Now for the identifier of the H2, the name becomes a copy of the H copy, because he is copied from the copy, the copy is the function f () Inner object, after the object is returned, the number of objects is temporarily changed to 3, but then the H copy is destroyed. */HowMany2 H2=f (h); //after the call to F () is complete, there are only two objects H and H2, which is a copy of the H2 that is finally the H copy.H2.print ("H2 after call to F ()");  out<<"Call F (), no return value"<<Endl; //call F (h) starting from line 15th, this time the call ignores the return value, and in 16 rows you can see exactly before the argument is passed in .//The copy constructor is called. As before, 21 rows show the copy constructor called in order to return a value. But the copy constructor is required//there is a working address for its destination address (this pointer), but where does this address come from? //whenever a compiler needs a temporary object in order to properly compute an invisible object, the compiler creates one, in which case//The compiler creates an invisible object as the function f () ignores the destination address of the return value. This temporary object's lifetime should be as short as possible,//this way the space will not be messed up by these temporary objects that are waiting to be destroyed and resource-intensive. In some cases, temporary objects may be passed to//another function, but now the temporary object is no longer needed in this case, so once the call is complete, the destructor is called on the inner object (23-24)//This temporary object is destroyed (25-26)//28-31 lines, H2,h is destroyed .f (h);  out<<"After call to F ()"<<Endl;}//Output Result:/*1.howmany2 () 2.h:objectcount=13.entering f ()//The Copy construction 4 occurs when the object is passed as a function parameter. HowMany2 (const howmany2&) 5.h copy:objectcount=2--------------------------6.x argument inside F () 7.h Copy:o Bjectcount=38.returning from F ()//Return object call copy constructor again--------------------------9. HowMany2 (const howmany2&) 10.h copy copy:objectcount=3//temporary object is used, destructor 11.~howmany2 () 12.h COPY:OBJECTCOUNT=213.H2  After call to F () 14.h copy Copy:objectcount=215.call f (), no return value//f (h) 16.howmany2 (const howmany2&) 17.h copy: objectcount=318.x argument inside F () 19.h copy:objectcount=320.returning from F ()//f (h) is assigned to an unknown value, where copy constructor 21 is called. HowMany2 (const howmany2&) 22.h Copy copy:objectcount = 4//Temporary object is used again call destructor 23.~howmany2 () 24.h copy:objectcount = 3// Destroying anonymous Objects 25.~howmany2 () 26.h copy Copy:objectcount=227.after call to F () 28.~howmany2 () 29.h copy copy:objectcount=130.~ HowMany2 () 31.h:objectcount=0*/

C + + knowledge Point: Copy constructor example

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.