Thursday, 24 January 2013

Understanding Copy Constructor

Copy Constructor

To create an object from an existing object requires a special type of constructor, called a copy constructor. It allocates memory but copies the POD (Plain Old Data) values from another object of the same class. It has one parameter, a reference to an existing object.
Like a default constructor, one is supplied by the compiler if there is a need for one and you haven't supplied it. If the object being copied is POD then no problems, i.e. no references, virtual functions or other complexities which affects how the data is stored in the object.
E.g. a pointer to non-const data owned by the object will be cloned and when the original object is destroyed it will release ownership of the pointer. So will destroying the new object. Freeing an object twice is a bad thing that can lead to unexpected crashes. So if you have non POD in your object then you must write a copy constructor and take care of the copying. For POD you can let the compiler do the hard work!
This example of a class containing POD shows a copy constructor being used.


 #include <iostream>
 
 using namespace std;
 
 struct example 
 {
     int rd; 
     example(example & ref) 
     {
         cout << "Copy Constructor called" << endl;
         rd = ref.rd;
     }
 
     example() : rd(0) 
     {
     }
 };
 
 int main() 
 {
     example s;
     s.rd =9;
     example s2=s;
     cout << s2.rd << endl;
     return 0;
 }
 

The previous page used a struct instead of a class. The two are identical except members in a struct have public access by default whereas class members are private by default.


 #include <iostream>
 #include <string>
 
 using namespace std;
 
 class demo 
 {
 
       int pd;
       string id;
    public:
       demo(demo & ref) 
       {
           cout << "Copy Constructor called" << endl;
           this->pd = ref.pd ;
           this->id = "Copy Constructed";
       }
 
       demo(string name) 
       {
           pd = 1;
           id=name;
           cout << "Constructor for " << id << endl;
       };
 
       ~demo() 
       {
           cout << "Destructor called "<< endl;
       }
 };
 
 int main() 
 {
   demo s("First");
   demo s2=s; //copy constructor called
   return 0;
 }
 
This example shows a class SomeData containing an int member pd. A copy constructor is provided to create a new instance of this pointer and then copy the original value into it. The id  is provided by the normal constructor but is just set to a nominal value in the copy constructor.
As we have allocated memory in the various constructors, it is vital that the memory is released when the object is destroyed so don't forget the destructor in that case.

1 comment:

Anonymous said...

Hi there! This is kind of off topic but I need some advice from an established blog.
Is it very difficult to set up your own blog? I'm not very techincal but I can figure things out pretty quick. I'm thinking about making my own but I'm not sure where to begin. Do you have any ideas or suggestions? Thank you

Also visit my site - anti cellulite treatment