GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 43 of 203    Print  
Difference between a "assignment operator" and a "copy constructor"

  
Total Answers and Comments: 3 Last Update: July 13, 2007     Asked by: suji 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: Lily
 

Copy constructor is called every time a copy of an object is made. When you pass an object by value, either into a function or as a function's return value, a temporary copy of that object is made.

Assignment operator is called whenever you assign to an object. Assignment operator must check to see if the right-hand side of the assignment operator is the object itself. It executes only the two sides are not equal.



Above answer was rated as good by the following members:
YaelG, pavithra.r24, Ajay Kanse, j_l_larson
September 28, 2005 00:57:57   #1  
Lily        

RE: Difference between a "assignment operator" and a "...

Copy constructor is called every time a copy of an object is made. When you pass an object by value either into a function or as a function's return value a temporary copy of that object is made.

Assignment operator is called whenever you assign to an object. Assignment operator must check to see if the right-hand side of the assignment operator is the object itself. It executes only the two sides are not equal.


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
June 14, 2006 22:48:30   #2  
Doreen        

RE: Difference between a "assignment operator" and a "...

If a class contains members that are pointers initialized by new then you should define a copy constructor that copies the pointed-to data instead of copying the pointers themselves. This is termed deep copying. The alternative form of copying (memberwise or shallow copying) just copies pointer values. A shallow copy is just that the shallow scraping off of pointer information for copying rather than the deeper mining required to copy the constructs referred to by the pointers.

The overloaded assignment operator is used when you assign one object to another existing object:

The solution for the problems created by an inappropriate default assignment operator is to provide your own assignment operator definition one that makes a deep copy. The implementation is similar to that of the copy constructor but there are some differences.

1. Because the target object may already refer to previously allocated data the function should use delete[] to free former obligations.

2. The function should protect against assigning an object to itself; otherwise the freeing of memory described previously could erase the object's contents before they are reassigned.

3. The function returns a reference to the invoking object.


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
July 13, 2007 11:38:05   #3  
Indrajit Paul        

RE: Difference between a "assignment operator" and a "...
Inspite of repeated requests by some (few) sensible people to everybody to read the question properly before responding nobody listens.

For doreen the question does not ask you to explain deep copy and shallow copy mechanism neither does is asks how assignment operator should be written.

It asks the difference between a assignment operator and a copy constructor.

The difference is a assignment operator (default or user provided) is called when the object already has been instantiated. A copy constructor is called when the object has not yet been constructed.

For Example.

Class A{/*...*/};

main()
{
A a;
A b a; /* copy constructor will be called */
A c;
c a; /* assignment operator will be called*/
}

 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape