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.
source share