"C + +" copy constructor

Source: Internet
Author: User

Reference: Huang Bongyong (inside the argument for temporary variables I do not understand, I feel there is a problem with the explanation)

To use the copy constructor:

1. Function value passing

2. Returning objects

3. Initializing another object with one object

Focus on the following two scenarios:

① only the copy constructor is called and no temporary variables are generated

A = b;

② generates temporary variables that call copy constructors and assignment functions

A;

A = b;

#include <iostream>using namespacestd;classhyong{ Public:    intA, B, C; Hyong () {a= b = c =0; cout<<"construct"<<Endl; } hyong (inti) {a= b = c =i; cout<<"Construct 2"<<Endl; } hyong (ConstHyong &obj)//copy Constructor{a= b = c =9; cout<<"Construct copy"<<Endl; }    voidH (hyong k)//Value Passing{cout<<"k.a ="<< k.a <<"k.b ="<< k.b<<Endl; } Hyong& Hyong::operator= (ConstHyong &Other ) {cout<<"="<<Endl; if( This= = &Other )return* This; A=Other.a; b=other.b; C=other.c; return* This; }    ~Hyong () {cout<<"Xigou"<<Endl; }};hyong f ()//return Object{hyong m3 (5); returnm3;}intMain () {Hyong m (1); Hyong N= m;//Call the copy constructorcout << M.A << m.b <<Endl; cout<< n.a << n.b <<Endl; Hyong M1= Hyong (m);//Call the copy constructorcout << m1.a << m1.b <<Endl;    Hyong m2 (m); //Call the copy constructorcout << m2.a << m2.b <<Endl; Hyong*p =NewHyong (m);//Call the copy constructorcout << p->a << p->b <<Endl; cout<<"------------"<<Endl;    Hyong M3; M3= Hyong (m);//A temporary variable is obtained by using the copy constructor, and then the temporary variable value is assigned to the M3 using the assignment function, and then the temporary variable is destructorcout <<"------------"<<Endl;    Hyong M4; M4= m;//Direct Call Assignmentcout <<"------------"<<Endl; Hyong M5= f ();//set F () to return the temporary variable r, actually r is the function of the M3,//M3 is assigned to M5 by means of a copy constructor, and then m3 the destructorcout <<"------------"<<Endl;          Hyong M6; //defined and initialized with the default constructor M6M6 = f ();//f () returns M3, then assigns the M3 to the temporary variable tmp with the copy constructor, M3 destruction;//TMP is assigned to M6 by the assignment function, and the TMP is destructorcout <<"------------"<<Endl;     Hyong M7 (f ()); //set F () returns M3,M3 assigned to M7 by a copy constructor, and then M3 destructor    return 0;}

C + + copy constructor

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.