Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ??

like image 537
Anerudhan Gopal Avatar asked Nov 29 '22 09:11

Anerudhan Gopal


2 Answers

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.

like image 152
iammilind Avatar answered Dec 15 '22 22:12

iammilind


C c4 = c1;

is Copy Initialization.

It tries to convert c1 to an type C if it is already of not that type by finding a suitable conversion function and then uses the the created C instance for copy constructing a new C instance.

Note that though,

C c4(c1);

is Direct Initialization

And it is important to note that there is a difference between Copy Initialization and Direct Initialization they are not the same!

Why C c4 = c1; is syntactically equivalent to C c4(C1) and not C C4; c4 = c1; Why was it made that way?
First, Copy Initialization & Direct Initialization are not the same.
As to the rationale of why no assignment operator is called, assignment only occurs when one assigns two completely formed objects to each other.

In case of:

C c4 = c1;

c1 is a completely constructed object while c4 yet does not exist all, When such an scenario arises and = is present, it doesn't really mean Assignment but it means Initialization.
So Basically, what happens here is Initialization and not Assignment and the C++ Standard defines specific rules as to how this initialization should be done.

like image 21
Alok Save Avatar answered Dec 15 '22 22:12

Alok Save