Talk:Copy constructor (C++)

message: I think, it will be great if someone with good c++ skills will modify Copy constructors and templates section, and write more about
"An explicit, non-template, copy constructor must also be provided for construction of Array from Array." with examples.I found this article with Google with hope to find hints about it, but it's not enough for me to understand it(may be i'm stupid? :) )
--Anonymous  —Preceding unsigned comment added by 93.178.67.13 (talk) 00:08, 1 October 2008 (UTC)
thread_title:  [Headline text] 

message: 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)
thread_title:  Deep copy 

message: 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)
thread_title: Section content

message: 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)
replies: text: Done. decltype 09:58, 6 March 2009 (UTC)
thread_title:  Category 

message: 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)
thread_title:  Qualifier order 

message: In the comments for example
  X a = X();
it is mentioned that the compiler will create a temporary object, then invoke the copy constructor X::X(const X&)
Not sure what the standard says, but with Visual Studio, the copy constructor is not invoked in the following example:
  class X {
  public:
    X() { printf("X::X()\n"); }
    X(const X& copy_from_me) { printf("X::X(const X&)\n"); }
  }
  ...
  X a = X();
instead the default constructor is called (and if only the copy constructor is defined, it doesn't compile).  — Preceding unsigned comment added by 2001:4898:80E8:EE31:0:0:0:4 (talk) 23:15, 19 August 2014 (UTC)
thread_title:  Example wrong?