IT Community - Software Programming, Web Development and Technical Support

Difference between a "assignment operator" and a "copy constructor"?

This is a discussion on Difference between a "assignment operator" and a "copy constructor"? within the C and C++ Programming forums, part of the Software Development category; Hi pals please let me know the Difference between a "assignment operator" and a "copy constructor"?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C and C++ Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 06-06-2007, 04:22 AM
vigneshgets vigneshgets is offline
D-Web Genius
 
Join Date: Mar 2007
Posts: 904
vigneshgets is on a distinguished road
Exclamation Difference between a "assignment operator" and a "copy constructor"?

Hi pals please let me know the Difference between a "assignment operator" and a "copy constructor"?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-11-2007, 04:45 AM
nssukumar nssukumar is offline
D-Web Sr.Programmer
 
Join Date: Feb 2007
Posts: 155
nssukumar is on a distinguished road
Default Re: Difference between a "assignment operator" and a "copy constructor"?

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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-12-2007, 05:30 AM
lavanya lavanya is offline
D-Web Trainee
 
Join Date: Jul 2007
Posts: 24
lavanya is on a distinguished road
Smile Re: Difference between a "assignment operator" and a "copy constructor"?

(I) Copy Constructor:
  • C++ calls a copy constructor to make a copy of an object
  • Copy constructor creates the new object by copying the existing one
  • Copy constructor always creates a new object , but the assignment never copies the object
  • If ther is no copy constructor created for the class,C++ creates the default copy constructor for all the fields it is called as shallow copy
EXAMPLE:
A variable is declared which is initialized from another object
Point p; // calls default constructor

point s=p; //calls copy constrctor

p=s; //assignment,not copy constructor

(II)Assignment Operator:
  • assignment operator assigns a value to a already existing object
  • The assignment operator makes an already existing object into a copy of an existing one

Thanks,
Lavanya.J
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
I keep getting "Data Missing" when I click the "back" button in my browser. How can I oxygen HTML, CSS and Javascript Coding Techniques 1 07-28-2007 01:12 AM
Diffrence between a "assignment operator" and a "copy constructor" Sabari C and C++ Programming 1 07-24-2007 05:00 AM
Difference between "printf" and "sprintf". Sabari C and C++ Programming 1 07-23-2007 06:23 AM
Why do I get "HTTP 500" error(or "(DLL)initialization routine failed")in my browser? kingmaker ASP and ASP.NET Programming 1 07-20-2007 04:38 AM
Difference between "APPLET" and "APPLICATION" prasath Java Programming 1 07-17-2007 07:51 AM


All times are GMT -7. The time now is 08:34 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0