Why is Copy Constructor called here instead of the usual constructor and overloaded assignment operator?

Possible duplicate:
Is there a difference in C ++ between initialization and direct initialization? Copy constructors and assignment operators

I have a C class in which I overloaded Normal, copy constructor and an assignment operator to print a trace of what is being called.

I wrote the following code snippets to check what gets 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 

It seems to overshadow me because in this code, although I initialize during the declaration, it is through an assignment operator, not a copy constructor .. Do I get it wrong?

+4
source share
3 answers

Because C c4 = c1; is syntactically semantically equivalent:

 C c4(c1); 

Both will refer to the copy constructor in this particular case. However, there is a subtle difference between “copy initialization” (1st syntax) and “direct initialization” (2nd syntax). Check out this answer.

Note In “unprofessional terms,” the variable (here c4 ) is built until the first one is met ; (or , 'for several objects); Before that, everything will be one or another type of constructor. Sub>

In the case of C c4 = c1; the compiler should not check the most unpleasant parsing .
However, you can disable syntax like C c4 = c1; by declaring the copy constructor explicit . In this case, any constructor can be made explicit, and you can prevent the = sign in the construct.

+14
source
 C c4 = c1; 

Copy initialization .

He tries to convert c1 to type C if he is no longer of that type by finding a suitable conversion function, and then uses the created C instance to copy, creating a new C instance.

Please note that although

 C c4(c1); 

Direct initialization

And it is important to note that there is a difference between Copy Initialization and Direct Initialization, they do not match!

Why C c4 = c1; syntactically equivalent to C c4(C1) , not C C4; c4 = c1; C C4; c4 = c1; Why was this done this way?
First, copy initialization and direct initialization do not match.
As for the rationale for why the assignment operator is not called, assignment occurs only when one assigns two fully formed objects to each other.

When:

 C c4 = c1; 

c1 is a fully constructed object, and c4 still does not exist. When such a scenario arises and = present, this does not really mean the assignment, but means initialization.
In principle, this is the initialization, not the assignment, but the C ++ standard defines specific rules regarding how this initialization should be performed.

+8
source

Because there is no assignment operator in:

 C c4 = c1; 

Like a comma, an equal sign can be either an operator or punctuation. In an expression, this is always an operator, but in the above initialization expression is just c1 ; the = sign is the punctuation that copy initialization, and not direct initialization, should be used. (Direct initialization will be

 C c4( c1 ); 

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

+1
source

Source: https://habr.com/ru/post/1412501/


All Articles