?
Solved

Copy constructor

Posted on 2004-04-27
5
Medium Priority
?
225 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 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this post we will learn different types of Android Layout and some basics of an Android App.
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 …
Progress

800 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