when copy constructor and overloaded assignment operator is called? The difference between the copy constructor and the assignment operator is confusing concept for a new programmer. To keep the concept with the mind remember the purpose. Let us discuss the purpose of copy constructor and assignment operator.

  • copy constructor is used to create an object from existing object ( already created ).
  • assignment operator can not create new object. it works only on already created objects. it means when we assign one already created object to an already existing object.

Let us see below example and its output.

Output

  • When passing an object by value, copy constructor is getting called.
  • When an object is returned from a function by value, copy constructor is getting called.

Ref:

http://www.csee.umbc.edu/~cress1/ia/Deliverables/appendix/202-CopyAssignment.PDF