"Venkatesh" <> wrote in message
news: oups.com
> John Carson wrote:
>> "Venkatesh" <> wrote in message
>> news: oups.com
>>> Ctors are implicit by default..which means this statement
>>> Test t2 = str; is no different from Test t2 (str);
>>
>> Incorrect. Implicit constructors means that
>>
>> Test t2 = str;
>>
>> is no different from
>>
>> Test t2 = Test(str);
>>
>> Subject to the qualification below, both of these involve a
>> constructor call to create a Test temporary from str, and a copy
>> constructor to copy this Test temporary to t2. Both are different
>> from
>>
>> Test t2(str);
>>
>> which does not involve a copy constructor. See section 12.6 of the
>> Standard.
>>
>> By section 12.8/15 of the standard, it is possible with both
>>
>> Test t2 = str;
>>
>> and
>>
>> Test t2 = Test(str);
>>
>> to omit the copy constructor call as an optimization.
>>
>
> May be, i should have elaborated a bit more.
> AFAIK, if something happens to go through an implicit ctor, then copy
> ctor will not be called which means
> Test t2 = str; is no different from Test t2 (str);
> where as Test t2 = Test(str) may skip or go through the copy ctor.
This is just plain wrong. As I have already stated,
Test t2 = str;
and
Test t2 = Test(str);
are the same when there is an implicit constructor. In either case the copy
constructor may or may not be called. In *both* cases, the copy constructor
is required to be accessible even if not called.
With
Test t2 (str);
by contrast, the copy constructor is *never* called and is not required to
be accessible.
> Also a closer look at the creation of these two objects will tell the
> fact that calls made for 'Test t2 = str' are exaclty the same as 'Test
> t2(str)'
Wrong. Compile them using Comeau online with a private copy constructor.
http://www.comeaucomputing.com/tryitout/
Observe that
Test t2 (str);
will compile but
Test t2 = str;
will *not* compile.
> Please refer Bjarne's TCPL section 11.3.4 where reads like complex x=2
> is equivalent to complex x(2) and also that complex y = complex(2,0)
> is equivalent to complex y(2, 0);
You are misreading this. Start in the previous section (p. 270), where he
says that
complex b = 3;
means
complex b = complex(3);
Then in section 11.3.4, he says that:
"In principle, copy constructors are used in simple initializations such as:
complex x = 2;
complex y = complex(2,0);"
Such initializations are therefore NOT equivalent to
complex y(2,0);
because it is NOT the case that "in principle, copy constructors are used"
in such initializations.
He also comments that: "It is possible to restrict the set of values
accepted by the = style of initialization compared to the () style by making
the copy constructor private", i.e., inaccessible, which is the point I have
made above.
--
John Carson