Solved

Copy constructor

Posted on 2004-04-27
5
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A short article about a problem I had getting the GPS LocationListener working.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Simple Linear Regression
Introduction to Processes

688 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