The time at which the CPP copy constructor occurs

Source: Internet
Author: User

If the first argument of a constructor is a reference to its own class type, and any additional arguments have default values, this constructor is a copy constructor

class numbered{ public :    numbered () {// constructor        0;    }    Numbered (const numbered& inputone) {// copy constructor        mysn =  INPUTONE.MYSN;         = Mysn +1;    } int mysn;};

The copy constructor has the following conditions:

#include <iostream>#include<string>#include<vector>using namespacestd;classnumbered{ Public: Numbered () {//constructor FunctionMYSN =0; } numbered (Constnumbered& Inputone) {//copy ConstructorMYSN =inputone.mysn; MYSN= Mysn +1; }intmysn;};voidf (numbered s) {cout<<s.mysn<<Endl;}intMain () {numbered A;//constructor FunctionNumbered b=a;//"1" copy constructorf (a);//"2" in the process of assigning a to the function f, a copy construction occurred//"3" returns an object from a function that returns a type other than a reference--like the 2nd//"4" Initializes an object in an array or a member of an aggregate class with a curly brace list (c++11)System ("Pause"); return 0;}

Operation Result:

As you can see from the Watch window: B.MYSN is 1 more than A.MYSN, which is caused by the copy constructor

As can be seen from the output, f (a) outputs 1, which is caused by the copy constructor.

There are four things about the copy constructor:

    Numbered b=a;//"1" copy constructor    F (a);//"2" in the process of assigning a to function f, a copy construction    //"3" returns an object from a function with a return type of non-reference-similar to the 2nd    //"4" with a curly brace list ( C++11) Initializes an object in an array or a member of an aggregation class

2nd, the 3rd most easy to ignore, should pay attention to.



The above is the basic content of the copy constructor, the following is a more complex example to analyze the difference between the copy constructor, constructor, etc.
#include <iostream>#include<cstdlib>using namespacestd;classa{ Public: A () {cout<<"Constructor called!"<<Endl; } A (inti): x (i) {cout<< I <<"Constructor called!"<<Endl; } A (ConstA &a) {x= a.x+1; cout<< x <<"copy Constructor called!"<<Endl; } A&operator=(ConstA &a) {x= a.x+Ten; cout<< x <<"copy Assignment function called!"<<Endl; return* This; }    ~a () {cout << x <<"destructor called"<<Endl;}Private:    //A (const a &a) {cout << "copy constuctor called!" << Endl;}    intx;}; A get_a () {A A (5); returnA;}intMain () {cout<<"-----1-----"<<Endl; A A=1;//1cout<<Endl; cout<<"-----2-----"<<Endl; A b= Get_a ();//2cout<<Endl; cout<<"-----3-----"<<Endl; b= A;//3cout<<Endl; cout<<"-----4-----"<<Endl; A C= A (2);//4cout<<Endl; cout<<"-----5-----"<<Endl; b=3;//5cout<<Endl; cout<<"-----6-----"<<Endl; A D= b;//6cout<<Endl; cout<<"-----7-----"<<Endl;     A e (b); //7cout<<Endl; return 0;}

Run results

For the 1th and 4th, why is there no copy constructor for this operation??

This place is not very clear?

Also please master advice. If I figure it out, I'll keep it up to date.

The time at which the CPP copy constructor occurs

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.