Talk:Copy constructor (C++)
![]() | Computing Start‑class Low‑importance | |||||||||
|
![]() | C/C++ Unassessed High‑importance | |||||||||
|
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
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)
A copy constructor is called whenever a new variable is created from an object. This happens in the following cases (but not in assignment).
A variable is declared which is initialized from another object, eg,
Person q("Mickey"); // constructor is used to build q.
Person r(p); // copy constructor is used to build r.
Person p = q; // copy constructor is used to initialize in declaration.
p = q; // Assignment operator, no constructor or copy constructor.
A value parameter is initialized from its corresponding argument.
f(p); // copy constructor initializes formal value parameter.
An object is returned by a function. C++ calls a copy constructor to make a copy of an object in each of the above cases. If there is no copy constructor defined for the class, C++ uses the default copy constructor which copies each field, ie, makes a shallow copy.
BY D Murli(India-Bhilai(C.G)) 25-6-2007 : 12:08:26
Category
Hello, Isn't this page a candidate for "Articles with example C++ code" category? Can some one please edit this to reflect the same. Thanks, Raoravikiran (talk) 09:11, 6 March 2009 (UTC)
Qualifier order
Note that it is a purely aesthetic change, however there was mixed usage throughout the article, so this change might reduce any confusion for those new to the language. In addition, although it violates the easy-to-read nature of right-to-left declarations, it is the far more common form, though I am too lazy to prove that. It is, however, the order that the standard uses so it is probably best to talk about qualifier order else-where, and focus on the copy constructors themselves here. To clarify the change I made is here: http://en.wikipedia.org/w/index.php?title=Copy_constructor&oldid=309822601 I was bold and hope this improves the article 216.94.43.50 (talk) 18:19, 24 August 2009 (UTC)