Click here to Skip to main content
15,920,111 members
Please Sign up or sign in to vote.
1.57/5 (3 votes)
See more:
Hi,

I don't know if i should be asking these type of question, by as a programmer i have simple question as why is the need of copy constructor.
I know the concept well and how to use the copy constructor, but i need a practical application where copy constructor is mandatory to be used, or i can re frame the question as In what condition we should be using copy constructor.

Thanks in Advance !!
Posted
Comments
nv3 4-Jul-14 6:38am    
Well, somehow we must have a way to tell the compiler how it can copy an object of our class into another. Such copy operations happen when we pass an object as a parameter to a function and several other situations. In such cases the compiler uses the copy constructor. And if you don't declare one, it creates a default version of to its best knowledge, which in simple words copies the object member by member.

For some classes this default copy constructor does not what we want to do. For example, imagine your class contains a pointer to another object and "owns" that object. In such cases you want to be able to specify what exactly should happen with the pointer value, i.e. do we want another object be created which is now owned by the copy? Or we might need to increment a reference count ...

Do you understand now, why it is necessary that we specify how objects should be copied?
Richard MacCutchan 4-Jul-14 7:24am    
You should post this as a solution; it's worth a 5.
nv3 4-Jul-14 9:47am    
Thanks, Richard. Yes, it got longer than I originally thought :-)

For instance (as far as I know) STL containers requires copy contructable objects.
Also, if you plan to overload the assignment operator then you should also impement the copy constructor.
 
Share this answer
 
Well, somehow we must have a way to tell the compiler how it can copy an object of our class into another. Such copy operations happen when we pass an object as a parameter to a function and several other situations. In such cases the compiler uses the copy constructor. And if you don't declare one, it creates a default version of it to its best knowledge, which in simple words copies the object member by member.

For some classes this default copy constructor does not what we want to do. For example, imagine your class contains a pointer to another object and "owns" that object. In such cases you want to be able to specify what exactly should happen with the pointer value, i.e. do we want another object be created which is now owned by the copy? Or we might need to increment a reference count ...

Do you understand now, why it is necessary that we specify how objects should be copied?
 
Share this answer
 
The copy constructor is needed in many situations, even if you do not call it explicitely. Some examples are:

1. Pass an object as a function argument by value
2. Return an object from a function by value
3. Create a temporary copy, e. g. when overriding the postifx operators ++ or --

while you can choose to work around the third case, and may (in fact, should!) pass a const reference instead of a value in the first case, the second case cannot be easily avoided.

Note that often, but not always, the assignment operator can be used instead of the copy constructor. these two are closely related.

See http://www.cplusplus.com/articles/y8hv0pDG/[^] for more info.
 
Share this answer
 
Hi, refer following link :
http://www.cplusplus.com/articles/y8hv0pDG/[^]
 
Share this answer
 
Comments
Stefan_Lang 10-Jul-14 4:56am    
That's the same link I posted 2 days ago :-)

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