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