?
Solved

Copy constructor

Posted on 2004-04-27
5
Medium Priority
?
234 Views
Last Modified: 2010-04-17
Hi

Why we use copy constructor explicitly. we have = operator to achive same reasult.
IS any differance in copy constructor and = operator.

sachin
0
Comment
Question by:123sachin
3 Comments
 

Expert Comment

by:jkps
ID: 10937433
As far as i know,
Copy cosntructors copy via initialization whereas Assignment operator copy via assignment alone.
In = operator,u just replace the older information in the object with the newer info.

So it depends upon the situ.,how and what u need to use
Hope this was of some use!!!
0
 
LVL 22

Accepted Solution

by:
NovaDenizen earned 80 total points
ID: 10938800
An = operator is automatically created for classes which do not explicitly provide their own.  Its behavior is to perform the '=' operation for each member of the object on the left and the right.  Classes need to create a copy constructor when the automatically generated one does not meet their needs.

Say you have a class with a memeber "char *buf;".  During initialization, you have "buf = new char[some_size]".  Let's say somebody does "obj1 = obj2", meaning an assignment happens between two objects of your class.  If you use the default assignment operator, then the 'buf' parameter was changed as if 'obj1.buf = obj2.buf' were executed.  This has two main problems.
1.  The buffer obj1.buf used to point to has leaked.  
2.  There are now two objects that point to obj2.buf.  When one of the objects is destroyed, it will free the buffer.  Then the other object will point towards an invalid buffer.

Basically, whenever a class contains pointers, or has external interactions during construction or destruction, you need to hand copy constuction and assignments properly.
0
 

Expert Comment

by:pcjunky
ID: 10988764
The copy constructor is used (in C++) when passing by value to a function to provide a "deep" copy of a class.  Where the = is used for assignment.  You can actually use the copy constructor when writting the = operator.

~proteus_82
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Introduction to Processes

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question