Jump to content

Talk:Copy constructor (C++)

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Gfaraj (talk | contribs) at 05:48, 23 October 2006. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Why do we need to use & in the definition of a copy constructor? --Anonymous


This article has an error regarding the implicit copy constructor. It does not perform a "byte-by-byte" copy of its members, since its members could have user-defined copy constructors as well. The standard (2003, 12.8.8) specifies:

The implicitly-defined copy constructor for class X performs a memberwise copy of its subobjects. The order of 
copying is the same as the order of initialization of bases and members in a user-defined constructor (see 12.6.2). 
Each subobject is copied in the manner appropriate to its type:

— if the subobject is of class type, the copy constructor for the class is used;
— if the subobject is an array, each element is copied, in the manner appropriate to the element type;
— if the subobject is of scalar type, the built-in assignment operator is used.

Virtual base class subobjects shall be copied only once by the implicitly-defined copy constructor (see
12.6.2).

I will correct this now. If someone has a reference that says that "trivial copy constructors" are defined as being just bitwise copies, please add that explanation (or give me the reference and I will add it).

--Gfaraj 04:18, 23 October 2006 (UTC)[reply]


I added a definition for the copy constructor as it is defined in the C++ standard. There is a bug in the example program that was provided for the explicit copy constructor. First, it is not allocating the correct length of characters: it should be strlen(dsgn) + 1. It's missing the "+ 1". Also, it's unsafe to call setDesignation with a string longer than the string used to construct the Employee instance. The default constructor does nothing to initialize the designation string either. Finally, the whole example is very unsafe and promotes incorrect C++ programming. I suggest it be replaced with a more effective example.

--Gfaraj 05:39, 23 October 2006 (UTC)[reply]