Jun 15, 2020 in C Plus Plus

What is the difference between a Copy Constructor and an Overloaded Assignment Operator?

1 Answer

0 votes
Jun 16, 2020

A copy constructor and an overloaded assignment operator basically serve the same purpose i.e. assigning the content of one object to another. But still, there is a difference between the two.


complex c1,c2;

c1=c2; //this is assignment

complex c3=c2; //copy constructor

In the above example, the second statement c1 = c2 is an overloaded assignment statement.

Here, both c1 and c2 are already existing objects and the contents of c2 are assigned to the object c1. Hence, for overloaded assignment statement both the objects need to be created already.

Next statement, complex c3 = c2 is an example of the copy constructor. Here, the contents of c2 are assigned to a new object c3, which means the copy constructor creates a new object every time when it executes.

Click here to read more about Loan/Mortgage
Click here to read more about Insurance

Related questions

+1 vote
Jul 16, 2019 in C Plus Plus