On Apr 16, 1:22 am, Marcin Swiderski <sfider.b...@[EMAIL PROTECTED]
> wrote:
> On 15 Kwi, 20:33, suresh <suresh.amritap...@[EMAIL PROTECTED]
> wrote:
>
> > base b1 = 'x'; //why copy constructor not called here?
>
> This line calls base(const char) constructor and that is the correct
> behaviour. This sytanx means that object b1 is initialized with
> value 'x'. It's equivalent to:
>
> base b1('x');
It's NOT equivalent, it only seems so most of the time.
The difference is that when using assignment syntax, the compiler has
flexibility on how it implements the initialization. It's allowed
either direct initialization (no tem****ary), or indirect
initialization (create a tem****ary and then copy construct.)
However, when you directly initialize b1 with the argument in
parenthesis, there is no possibility of the extra tem****ary, because
the language doesn't allow for it.
Depending on compiler (or your optimization leve) you may get
different behavior:
base b1('x'); // absolutely no tem****ary
base b1 = 'x'; // may generate tem****ary+copy, may not
This is the reasoning behind the advice of prefering the first form of
initialization (using parenthesis) over the second (assignment
syntax.)
--
Chris
--
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|