C Interview Questions and Answers
Question: Assignment Operator - What is the diffrence between a "assignment operator" and a "copy constructor"?Answer:Answer1. In assignment operator, you are assigning a value to an existing object. But in copy constructor, you are creating a new object and then assigning a value to that object. For example: complex c1,c2; c1=c2; //this is assignment complex c3=c2; //copy constructor Answer2. 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. |
Is it helpful?
Yes
No
Most helpful rated by users:
- What is C++?
- What is function overloading and operator overloading?
- How do you find out if a linked-list has an end? (i.e. the list is not a cycle)
- What is the difference between declaration and definition?
- What is the difference between realloc() and free()?