Wednesday, 5 February 2014

Copy constructor in c++


What is copy constructor?

As the name suggests the constructor that used to copy the value of the one object to the other object while creating is called as copy constructor . So we are calling it  as a copy constructor.  Copy constructor, if not defined by the programmer then compiler will create its own. 
In this case normal constructor will not be called and as normal constructor copy constructor will not have return type and have same name as class name.
In other words,
what is constructor? It is used to initialize the objects .
what is copy constructor? It is used to initialize the objects with other objects while creating it.

When copy constructor is called ?

Ø  when you pass an “object as a value” copy constructor is called. Don’t just confuse with the object as value. It is same as call by value. Here instead of variable we were using the objects.
Fruits apple;  //Fruit is class, apple is object
callme(apple);  //object as value
The above code is similar to the following….
int a;
callme(a); //variable as value

I hope you have understand the difference between these two. so, coming to our topic copy constructor is called whenever you pass an object as a value.
And another important noting point is the copy constructor will not call again and again when we use object as a value. It will call only at  the time of object creation as above mentioned example.

Ø  when you return a object in function, the copy constructor will be called.
Fruits callme(Fruits apple)
      return apple;   // returning object of class Fruit

Ø  Next whenever you initialize the object with other object the copy constructor will be called.
Fruits apple, orange;
apple=orange; // Initializing orange object with apple
Fruits pinapple(apple); //Here too copy constructor called

Syntax for copy constructor :
class myclass
           myclass (const myclass &obj_passed)
          //copy constructor code
          //create a new copy of object here
In above syntax :
obj_passed is the reference of the object from which we are going to copy the value.  Note the const keyword in parameter.

What happens if copy constructor is not created by user?
So, are you wondering what happens if copy constructor is not created by the user. You just don’t worry compiler will create it. Even if copy constructor is not defined the following code will work.
Fruit apple=orange;
where orange and apple are objects and fruit is class. Here we were assigning a orange object with the apple. In a compiler defined copy constructor, initialization of the object takes place “member wise”.  This process is called is called as Shallow Copy. Hope you understand member wise, Data members of the source object will be copied to destination object’s Data members.

Where the copy constructor is must?

When we fail to create a copy constructor the compiler will create it by own. In such cases if member of the object is pointer then address will be copied in the object created by the copy constructor. So object created by class and a copy constructor will share a same memory location. In such case when destructor is called, no problem for the 1st object, memory will be deallocated but in the second case the application will crash. So in case of pointers the copy constructor is must.