Talk:Copy constructor
From Wikipedia, the free encyclopedia
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)
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)
[edit] Deep copy
I have moved this from the article because it needs work:
- it needs tidying up for tone and style
- I don't think it actually compiles
- it appears to repeat deep copy and is probably unnecessarily detailed for this article as well as repeating the preceeding example
[edit] Section content
Deep copy is a way how the data inside the objects are copied during invocation of copy constructor or assignment operator. The essence of deep copy is that it copies all data inside the object (not only pointers on the data). Other ways doesn't copy dynamic alocated data, they create a bit copy of first object, so the copied pointers inside the new object still refers to the memory of the first one. In same cases it might be a problem, which you can solve using deep copy. What do you need to create a deep copy?
- Constructor - It creates internal dynamic data structures
- Destructor - It destroys internal dynamic data structures
- Copy constructor - It creates internal dynamic data structures and makes a copy of internal data of the objects
- Overload assigment operator - It makes a copy of internal data of the objects
Example of deep copy implementation:
# include <iostream> using namespace std; class deep_copy { int *dynamic_data; public: /* Constructor initializes internal data structures */ deep_copy(int data) { dynamic_data = new int; *dynamic_data = data; } /* Copy constructor initializes internal data structures and copies all data from the other object to self */ deep_copy(const deep_copy& dc) { if (dynamic_data == NULL) dynamic_data = new int; if (this != &dc) *dynamic_data =* (dc.dynamic_data); } /* Overloaded assigment operator which copies all data from the other object to self */ deep_copy& operator=(const deep_copy& dc) { if (this != &dc) *dynamic_data = *(dc.dynamic_data); return *this; } /* Destructor which delete all dynamic allocated data */ ~deep_copy() { delete(dynamic_data); } /* Prints data inside object - only for demonstration of functionality */ void vypis() { cout << *dynamic_data; } }; int main(int argc, char *argv[]) { deep_copy *x = new deep_copy(3); // I create new dynamic number deep_copy *y = new deep_copy(0); // I create new dynamic number *y = *x; // I make a deep copy, so y = 3 *x = 4; // Using a copy constructor i set x to 4 x->vypis(); // Prints 4 y->vypis(); // Prints 3 - that's because i have made a deep copy return 0; }
Rich257 10:07, 30 October 2006 (UTC)
Yes, I agree. A better example should be written for this article. Why does the above example allocate a deep_copy in the free store? It is an unnecessary (and misleading) complication that contributes nothing to this article. --Gfaraj 04:24, 5 November 2006 (UTC)
[edit] The examples needed replacement
In my opinion, the examples weren't adequate for this encyclopedia article. I've replaced them with simpler, more to the point, examples. Please feel free to comment if you still find them inadequate.
--Gfaraj 06:32, 5 November 2006 (UTC)