472,374 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 software developers and data experts.

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 2053
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Christoph Bartoschek | last post by:
Hi, gcc 3.4 rejects the following program: class T { public: T() : a(3) {} explicit T(T const & other) : a(other.a) {} private: int a;
12
by: Marcelo Pinto | last post by:
Hi all, In practice, what is the diference between a default constructor and an explicit default constructor? class Ai { public: Ai() {} };
9
by: Tanmoy Bhattacharya | last post by:
Hi, This is a question about whether I am right that a particular syntactic sugar is missing in C++. Let me explain with an example. Let us say I have a class for complex numbers, and I want...
8
by: trying_to_learn | last post by:
Why do we need to explicitly call the copy constructor and the operator = , for base class and member objects in composition? ....book says "You must explicitly call the GameBoard copy-constructor...
2
by: Fred Zwarts | last post by:
Consider the following code: // Start of code class MyException_t { public: // Default constructor.
12
by: Ole Nielsby | last post by:
Why is this? I've stumbled on this restriction more than once, and I'd like to know the philosophy behind it if there is one. I figure I'd be less prone to make this error if I knew the reason....
1
by: petschy | last post by:
hello, i've run into an error when qualifying a copy ctor 'explicit'. the strange thing is that i get a compiler error only if the class is a template and declare the variable as X<Zx = y....
42
by: coder_lol | last post by:
Thanks everyone again for contributing to helping me clear C++ confusions. I did some serious reading on copy constructors and assignments and I think I've got a good handle on the memory stuff. ...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.