Copy constructor and assignment operator invocation cases

Source: Internet
Author: User

In many cases, it is difficult to accept the following instructions

int a (10);

is actually exactly equivalent to int a=10;

So the following situation is more difficult to accept:

Class a;//defines class A

Application:

A;

A B (a);//is actually equivalent to a b=a, the call is copy constructor, in everyone's eyes if not, very familiar with the way of such assignment, it is really considered an assignment operation.

Examples are as follows:

void Test (A A)

{

}

In this function, by passing the value, the copy constructor is actually called, not the assignment operator, or the default constructor.

Class t{public:    t ()     {        cout<< "T ()" <<endl;    }    t (const t&  T)     {        cout<< "call copy  Constructor "<<endl;    }    t operator = (T t)     {        cout<< "call assign  Constructor "<<endl;    }    ~t ()     {        cout<< "~t ()" <<endl;    }private:};void The detailed invocation of   test (t t) {} is as follows:         t *p=new t ();         t c=*p;        t  cc;        cc=*p;        cout<< "* *" <<endl ; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;T&NBSP;BB (CC);         cout<< "* * *" <<endl;        test (*p);         delete p;


Copy constructor and assignment operator invocation cases

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.