Difference Between Copy Constructor and Assignment Operator in C++

C++Server Side ProgrammingProgramming

In this post, we will understand the difference between copy constructor and assignment operator in C++.

Copy Constructor

  • It is an overloaded constructor.

  • It initializes the new object with an already existing object data/value.

  • It is used when a new object is created with the help of some existing object.

  • Both these objects would be stored in separate memory locations.

  • If no copy constructor is defined inside the class, the compiler provides one on its own.

Assignment Operator

  • It is an operator.

  • It assigns the value of one object to another object, where both of these objects would already exist.

  • It is used when it is required to assign an existing object to a new object.

  • There is used a single memory location to store this object.

  • But there are multiple reference variables used to point to this object’s location.

  • If this operator is not overloaded, the bitwise copy will be created.

raja
Published on 24-Mar-2021 13:18:55
Advertisements