469,654 Members | 1,827 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,654 developers. It's quick & easy.

explicit copy constructors

Hello NG,

Can anybody fathom the purpose of an explicit copy constructor? On page 232
of the Josuttis STL reference, I see a reference to such.

How could you ever need to supress the possibility of an implicit conversion
from type T to type T? Such an implicit conversion could never occur
because you're already of the required type!

Thanks,
Dave
Jul 22 '05 #1
2 1944
Dave wrote:
Can anybody fathom the purpose of an explicit copy constructor? On page 232
of the Josuttis STL reference, I see a reference to such.

How could you ever need to supress the possibility of an implicit conversion
from type T to type T? Such an implicit conversion could never occur
because you're already of the required type!


IIRC, an explicit copy constructor would prevent copying in certain
cases. I strongly urge you to look in the archives about 'explicit'.
I do remember some interesting discussions in c.l.c++.moderated about
'explicit' keyword and its use for non-conversion constructors.

V
Jul 22 '05 #2
Dave wrote:
Hello NG,

Can anybody fathom the purpose of an explicit copy constructor? On page 232
of the Josuttis STL reference, I see a reference to such.

How could you ever need to supress the possibility of an implicit conversion
from type T to type T? Such an implicit conversion could never occur
because you're already of the required type!

When such conversion produces undesirable results, as in the case of
containers.
Check TC++PL 11.7.1, on page 284. A quote from there:
"By default, a single argument constructor also defines an implicit
conversion. For some types, that is ideal. For example:

complex z = 2; // initialize z with complex(2)
In other cases, the implicit conversion is undesirable and error-prone.
For example:

string s= ´a´; // make s a string with int(’a’) elements

It is quite unlikely that this was what the person defining s meant.
Implicit conversion can be suppressed by declaring a constructor
explicit. That is, an explicit constructor will be invoked only
explicitly. In particular, where a copy constructor is in principle
needed (11.3.4), an explicit constructor will not be implicitly invoked."


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Christoph Bartoschek | last post: by
12 posts views Thread by Marcelo Pinto | last post: by
9 posts views Thread by Tanmoy Bhattacharya | last post: by
8 posts views Thread by trying_to_learn | last post: by
2 posts views Thread by Fred Zwarts | last post: by
12 posts views Thread by Rahul | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.