0 votes
in C Plus Plus by

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

1 Answer

0 votes
by

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.

Example:

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.

Related questions

+1 vote
asked Jul 16, 2019 in C Plus Plus by Indian
+2 votes
0 votes
asked Jun 1, 2022 in JAVA by SakshiSharma
...