Why Copy Constructor is called here instead of normal Constructor and overloaded assignment operator?

c++, copy-constructor

Solution

Because `C c4 = c1;` is syntactically semantically equivalent to:

C c4(c1);

Both would invoke copy constructor in this particular case. However there is a subtle difference between "copy initialization" (1st syntax) and "direct initialization" (2nd syntax). Look at this answer.

Note: In "layman's terms", a variable (here `c4`) is constructed till the first `;` (or `,`' for multiple objects) is encountered; Till then everything is one or the other type of constructor.

In case of `C c4 = c1;`, compiler doesn't have to check for most vexing parse. However one can disable `C c4 = c1;` kind of syntax by declaring copy constructor `explicit`. For that matter any constructor can be made explicit and you can prevent `=` sign in the construction.

Problem

Possible Duplicate: Is there a difference in C++ between copy initialization and direct initialization? Copy constructors and Assignment Operators I have a class C in which I have overloaded Normal, copy constructor and assignment operator to print a trace of what is being called.. I wrote following pieces of code to test what is being called when? ``` C c1; --> Normal Constuctor .. // understood Fine C c2; c2 = c1; --> Normal constructor + assignment operator .. //understood Fine C * c3 = new C(C1) --> Copy constructor // Understood Fine C c4 = c1 --> copy constructor // Not Able to understand ``` This seems to baffle me since in this code though I am initializing at the time of declaration, it is through assignment operator and not copy constructor .. Am I understanding it wrong ??

Original source

Related problems