Click here to Skip to main content
15,942,292 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

I had gone through number of articles, but unable to get any concrete idea on both.

I mean to say -
- Why we need [practical usage] these two?
- Is there any substitution to these two?
- When it is compulsory to use them?

Kindly please guide me in detail (Practical usage\main advantages).

Updated 1-Jun-11 22:01pm
Dalek Dave 2-Jun-11 3:40am    
Edited for Grammar and Readability.
CPallini 2-Jun-11 4:00am    
You should read instead a good C++ book.

I Think was explained lot of times in CP.But not by me so i am taking this one,
A copy constructor is a special constructor that initializes a new object from an existing object.Compiler will creates a default copy constructor which copies your class data bit by bit, if you wont create it.Then why we have to re-create a Copy constructor?consider this class(not a professional code).
Class A
    public int *p;
    A(){ p = new int;}
         delete p;
int main()
   A a;
   A b = a; //default copy constructor provided by compiler,which exactly copies a.p pointer to to b.p;
   return 0;

But there is one big problem.when main is going to die,it will clean all stack variables.So may be first "a" destructor will be called and "a.p" is deleted.Next b destructor will be called,which again tries to delete b.p which is already deleted in a(Note thst a.p and b.p pointes same pointer).This will cause an run time error.To overcome this problem we have to create a copy aonstructor and Assignement operator(which is called in different scenarios).So the Class A copy constructor will be,
class A::A(const class A& RefA)
   p = new int;    // create a new p 
   *p = *RefA.p; // copy the value

So now both a, b in the above example will have different p pointers.Assignement operator functionality is also same.But there aim is defferent.

There are three general cases where the copy constructor is called instead of the assignment operator:
1. When instantiating one object and initializing it with values from another object (as in the example above).
2. When passing an object by value.
3. When an object is returned from a function by value.

In other cases like,
A a;
a = b; 

an Assignment operator is called
Share this answer
hakz.code 2-Jun-11 3:19am    
kudos!My 5
Thanks Harish
Dalek Dave 2-Jun-11 3:41am    
Good Call.
Thank you.
LaxmikantYadav 2-Jun-11 4:45am    
Good answer
We need Copyconstructor to initialize one object from other object, And generally we use assignment operator to invoke copy constructor.
Share this answer
Dalek Dave 2-Jun-11 3:41am    
Good answer.
LaxmikantYadav 2-Jun-11 4:42am    
Thanks :)
Hi venkat

thanks for assisting laxmikanth ( actually you saved my lot of time of typing and explaining your case when we need Copy constructor / assignmnet oprator )

Venkat has explained the case of Deep Copy ( where we need to take a copy of object which has pointer datamember )

Another need of Copy constructor / assignment operator comes when we are designing our class as "SingleTon"

SingleTon: a class which can have only one exclusive object thrughout the application session

so in the case when our class should not allow 2nd copy of object then we have to override Copy constructor / assignment operator so that client can not create another instance of the call by copying/assining the abject to a new one

Thank You
Share this answer
To override the copy constructor / assignment operator to stop the creation of another instance of the class place them in the private part of your class, and leave out the body.
class A {
    // Our copy constructor is private
    A(const A&);
    // Our assignment operator is private
    A& operator=(const A&);
    // Default constructor
    A() {}
    // Destructor
    ~A() {}
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900