Taras_96
12/1/2008 2:11:00 PM
On Oct 24, 12:38 am, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> No. By definition, the form
>
> TypeA objA = objB;
>
> is a shorthand for
>
> TypeA objA((TypeA(objB)));
>
> The syntax with the '=' is *not* direct initialisation, it is
> copy-intialisation from a *temporary object*, which is in turn
> directly initialised from 'objB'.
>
> In reality the compiler most likely skips the temporary and makes
> the code analogous to
>
> TypeA objA(objB);
>
> but the difference is that the copy constructor in 'TypeA' *must*
> exist and be accessible. That's the requirement.
>
So in the following passage: "The other (inefficient) way to build
constructors is via assignment, such as: Fred::Fred() { x_ =
whatever; }. In this case the expression whatever causes a separate,
temporary object to be created, and this temporary object is passed
into the x_ object's assignment operator. Then that temporary object
is destructed at the ;. That's inefficient. "
Does the author mean that:
x_ = whatever;
is equivalent to
x_.operator=(XConstructor(whatever));
?
So the first inefficiency mentioned is the right hand operand of the
assignment above. The second inefficiency mentioned is that the x_
will be default initialised, which may be wasteful. Is this correct?
I imagine that using an initialisation list would prevent both
inefficiencies, as x_ is no longer default initialised, nor is copy
assignment required (as x_ is initialised by 'whatever') .
Thanks again
Taras