Solved

Copy constructor

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now