What's the difference between the following two declarations, assuming I have not specified a copy constructor and operator=
in class Beatle
?
Beatle john(paul);
and
Beatle john = paul;
Edit:
In objects assignment, the operator =
implicitly calls the copy constructor unless told otherwise?
They're different grammatical constructions. The first one is direct initialization, the second is copy initialization. They behave virtually identically, only that the second requires a non-
explicit
constructor.*Neither has anything to do with the assignment operator, as both lines are initializations.
To wit:
const int i = 4;
is fine, butconst int i; i = 4;
is not.*) More accurately: The second version does not work if the relevant constructor is declared
explicit
. More generally, thus, direct-initialization affords you one "free" conversion:To address your edit: To understand the assignment operator, just break it up into parts. Suppose
Foo
has the obviousoperator=(const Foo & rhs)
. We can sayx = y;
, which just calls the operator directly withrhs
beingy
. Now consider this:First is Direct Initialization & second is Copy Initialization.
Direct initialization means the object is initialized using a single (possibly conversion) constructor, and is equivalent to the form
T t(u);
:Copy initialization means the object is initialized using the copy constructor, after first calling a user-defined conversion if necessary, and is equivalent to the form
T t = u;
:Copy Initialization does not work if the constructor is declared
explicit
.References:
This entry in Herb Sutter's GOTW should be a good read.
To answer your edited Question:
=
has a different meaning in depending on how it is used.If
=
is used in an expression in which the object is being created and initialized at the same time, then=
is not treated as Assignment Operator but as Copy Initialization.If
=
is being used to assign one object to another, after the object has been created then it results in call to Assignment Operator.