[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]


    Search the Q&A Archives


...difference between copy constructor and assignment...

<< Back to: Available C++ Libraries FAQ

Question by srinu naik
Submitted on 3/8/2004
Related FAQ: Available C++ Libraries FAQ
Rating: Rate this question: Vote
what is difference between copy constructor and assignment operator



Answer by Dip
Submitted on 5/1/2004
Rating:  Rate this answer: Vote
Copy constructor  copies a existing object to a non existing object, which you are going to create. Assignment operation can happen between two existing objects.

 

Answer by Subhash
Submitted on 5/8/2004
Rating:  Rate this answer: Vote
The copy constructor is creating a new object.
The assignment operator has to deal with the existing data in the object.  For a simple class, the work done may be the same, but for more complex classes, especially those with pointers to other objects, we will have more to do.  This is related to the deep and shallow copy issue, and ownership and is-a-part relationships.

 

Answer by sunil
Submitted on 1/5/2005
Rating: Not yet rated Rate this answer: Vote
copy constructor creates shallow copy
assignment operator creates deep copy.

 

Answer by vandy
Submitted on 6/12/2005
Rating: Not yet rated Rate this answer: Vote
The copy constructor is called at initialising the object while the assignment operator is used to assign one object to another.

 

Answer by rashmi
Submitted on 8/30/2005
Rating: Not yet rated Rate this answer: Vote
rashmi is good girl

 

Answer by manish bajaj
Submitted on 9/6/2005
Rating: Not yet rated Rate this answer: Vote
Copy constructor donot return anything.
Assignment operator returns object of same type

 

Answer by shobha
Submitted on 9/27/2005
Rating: Not yet rated Rate this answer: Vote
Assignment  and Copy constructor  are different.The copy constructor  initializes uninitialized  memory,whereas  assigenment  
starts with an existing initialized objects.

example

class  A {
public:
    A ( const A&); // copy ctor
    A& operator=(constA&);//assignment ctor
private:
int i;
};

if you make object like

A a1, a2;//default  ctor
A a3(a2);//copy ctor
A a2=a1;//calls copy ctor
a3=a2;//calls assignment ctor
      

 

Answer by Nitin Daule
Submitted on 10/11/2005
Rating: Not yet rated Rate this answer: Vote
When we use dynamic allocation for class member then if we use assignment operator
both object will point same memory, if we change one , changes reflect to other too.

 

Answer by Mukul Garg
Submitted on 11/28/2005
Rating: Not yet rated Rate this answer: Vote
Although it is right that copy constructor is used to create a new object with the existing one and assignment operator deals with two existing objects.
But when we use assignment operator between two objects the compiler uses the copy constructor for that.

 

Answer by Gunjan Goel
Submitted on 12/4/2005
Rating: Not yet rated Rate this answer: Vote
Copy Constructor comes in to picture when you want to create a new object from an exisiting object which is having a pointer as a data member.If you simply used to assignment operator the pointer of the old object will copy to the new object pointer and if old object goes out of scope then the pointer of the new object will point no where, so we use copy constructor so that we can make new pointer to point to the memory location for new object.

 

Answer by sachin pate
Submitted on 12/17/2005
Rating: Not yet rated Rate this answer: Vote
Copy constructor  copies a existing object to a non existing object, which you are going to create. Assignment operation can happen between two existing objects.





 

Answer by Niranjan Rewatkar
Submitted on 4/5/2006
Rating: Not yet rated Rate this answer: Vote
Copy constuctor initialize the object with the another object of same class whereas assignment operator can be called on objects of different classes

 

Answer by Bhagwat Bhakuni
Submitted on 5/16/2006
Rating: Not yet rated Rate this answer: Vote
1. class abc
2. { .......
3.   .......
4. };
5.
6. void main()
7. {
8.   abc a=new abc();
9.   abc b=a;
10.  abc c=new abc();
11.  c=a;
12. }

Hi All I M Posting The small Code Fragment To Understand Diffrence Between Copy Constructor And Assignment Operator.
Here In Line Number 9 Copy Constructor Will Be Called And In Line Number 11 Assignment Operator Will Be Called.
Hope It Will Help!!!

 

Answer by adi
Submitted on 5/25/2006
Rating: Not yet rated Rate this answer: Vote
Assignment operators does Self Assignment check but the copy constructor does not do that. Infact since copy constructor creates an non existing object it does not need to do a self assignment but then this is also a difference.

 

Answer by Anatoly
Submitted on 6/21/2006
Rating: Not yet rated Rate this answer: Vote
A copy constructor just builds an object yet returns nothing. An assignment operator returns *this, i.e. pre-existing object modified with that assignment operator.

 

Answer by zjiqi0
Submitted on 7/25/2006
Rating: Not yet rated Rate this answer: Vote
A copy constructor is used to initialize a newly declared variable from an existing variable. This makes a deep copy like assignment, but it is somewhat simpler:

   1. There is no need to test to see if it is being initialized from itself.
   2. There is no need to clean up (eg, delete) an existing value (there is none).
   3. A reference to itself is not returned.

 

Answer by zjiqi0
Submitted on 7/25/2006
Rating: Not yet rated Rate this answer: Vote
A copy constructor is used to initialize a newly declared variable from an existing variable. This makes a deep copy like assignment, but it is somewhat simpler:

   1. There is no need to test to see if it is being initialized from itself.
   2. There is no need to clean up (for example, delete) an existing value (there is none).
   3. A reference to itself is not returned.


 

Answer by Tella Marutheeswara Reddy
Submitted on 11/9/2006
Rating: Not yet rated Rate this answer: Vote
A copy constructor is used to initialize a newly declared variable from an existing variable. This makes a deep copy like assignment, but it is somewhat simpler:

There is no need to test to see if it is being initialized from itself.
There is no need to clean up (eg, delete) an existing value (there is none).
A reference to itself is not returned.

 

Answer by Elaine
Submitted on 4/3/2007
Rating: Not yet rated Rate this answer: Vote
A defined copy constructor is absolutely necessary when the the object's class parameters contain pointer variables with dynamically allocated memory. A copy constructor creates a deep copy as opposed to a shallow copy and is called when another object is created from another object of the same type, when an object is returned by a function, and when an object is passed by value as a function parameter. The difference between them is that a copy constructor is used to initialize a newly declared object from an existing object and an assignment operator is necessary when the two object already exist.

 

Your answer will be published for anyone to see and rate.  Your answer will not be displayed immediately.  If you'd like to get expert points and benefit from positive ratings, please create a new account or login into an existing account below.


Your name or nickname:
If you'd like to create a new account or access your existing account, put in your password here:
Your answer:

FAQS.ORG reserves the right to edit your answer as to improve its clarity.  By submitting your answer you authorize FAQS.ORG to publish your answer on the WWW without any restrictions. You agree to hold harmless and indemnify FAQS.ORG against any claims, costs, or damages resulting from publishing your answer.

 

FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personal opinion of the poster. These posts are not intended to substitute for medical, tax, legal, investment, accounting, or other professional advice. FAQS.ORG does not endorse any opinion or any product or service mentioned mentioned in these posts.

 

<< Back to: Available C++ Libraries FAQ


[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]

© 2008 FAQS.ORG. All rights reserved.