Solved

Copy constructor

Posted on 2004-04-27
5
215 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
5 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 20 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

828 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