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

谁说我不能喝 提交于 2019-11-27 16:53:22

问题


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


回答1:


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.




回答2:


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.




回答3:


Because there's no assignment operator in:

C c4 = c1;

Like the comma, the equal sign can be either an operator or punctuation. In an expression, it is always an operator, but in the above, the initialization expression is simply c1; the = sign is punctuation, indicating that copy initialization, rather than direct initialization, is to be used. (Direct initialization would be

C c4( c1 );

and is, IMHO, generally preferable. In the case where the initialization expression has the same type as the new object, however, there is no difference.)



来源:https://stackoverflow.com/questions/10595451/why-copy-constructor-is-called-here-instead-of-normal-constructor-and-overloaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!