• Home   /  
  • Archive by category "1"

Copy Constructor Vs Assignment Operator Definition

Question

In C++ what is the difference between copy constructor and assignment operator.  If they are created for you by default then why do we need to create them in the first place.

Assignment operator vs copy constructor

Copy constructors and assignment operators are created by default even if you do not implement your own. Copy constructors are called whenever an object variable is declared and initialized from another object including the cases when object variable is passed as a function value parameter or when object variable is returned by value from a function.

The difference between copy constructor and assignment operator is that assignment operator is used to copy the values from an object to another already existing object while copy constructor initializes a new object that does not exist before from another object.

We need to implement our own copy constructors and assignment operators simply because default ones do shallow copy or member wise copy which is not enough to clone any object for example if the object contains a pointer to a dynamically allocated memory then shallow copying produces two objects pointing to the same memory. If both objects try to free that memory then this will lead to problems.

If you have any comments, please use the comments section below for questions, corrections or feedback. Thanks for reading.

Search Terms...

Like this:

LikeLoading...

Tags:Cpp

Copy constructor and assignment operator, are the two ways to initialize one object using another object. The fundamental difference between the copy constructor and assignment operator is that the copy constructor allocates separate memory to both the objects, i.e. the newly created target object and the source object.The assignment operator allocates same memory location to the newly created target object as well as source object.

Let us study the difference between the copy constructor and assignment operator.

Content: Copy Constructor Vs Assignment Operator

  1. Comparison Chart
  2. Definition
  3. Key Differences
  4. Conclusion

Comparison Chart

Basis for ComparisonCopy ConstructorAssignment Operator
BasicThe copy constructor is an overloaded constructor.The assignment operator is a bitwise operator.
MeaningThe copy constructor initializes the new object with an already existing object.The assignment operator assigns the value of one object to another object both of which are already in existence.
Syntaxclass_name(cont class_name &object_name) {
//body of the constructor
}
class_name Ob1, Ob2;
Ob2=Ob1;
Invokes(1)Copy constructor invokes when a new object is initialized with existing one.
(2)The object passed to a function as a non-reference parameter.
(3)The object is returned from the function.
The assignment operator is invoked only when assigning the existing object a new object.
Memory AllocationBoth the target object and the initializing object shares the different memory locations.Both the target object and the initializing object shares same allocated memory.
DefaultIf you do not define any copy constructor in the program, C++ compiler implicitly provides one.If you do not overload the "=" operator, then a bitwise copy will be made.

Definition of Copy Constructor

A “copy constructor” is a form of an overloaded constructor. A copy constructor is only called or invoked for initialization purpose. A copy constructor initializes the newly created object by an another existing object. When a copy constructor is used to initialize the newly created target object, then both the target object and the source object shares different memory location. Changes done to the source object do not reflect in the target  object. The general form of the copy constructor is

class_ name (class_name &object_name){ . // body of copy constructor . } // object_name refers to the object on the right-hand side of the initialization.

If the programmer does not create a copy constructor in a C++ program, then the compiler implicitly provides a copy constructor. An implicit copy constructor provided by the compiler does the member-wise copy of the source object. But, sometimes the member-wise copy is not sufficient, as the object may contain a pointer variable. Copying a pointer variable means, we copy the address stored in the pointer variable, but we do not want to copy address stored in the pointer variable, instead we want to copy what pointer points to. Hence, there is a need of explicit ‘copy constructor’ in the program to solve this kind of problems.

A copy constructor is invoked in three conditions as follow:

  • Copy constructor invokes when a new object is initialized with existing one.
  • The object passed to a function as a non-reference parameter.
  • The object is returned from the function.

Let us understand copy constructor with an example.

class copy{ int num; public: copy(){ } //default constructor copy(int a){ //initializing constructor num=a; } copy( copy &c ){ //Copy constructor num = c.num; } void show( ){ cout<< num; } }; int main(){ copy A(200); //Object A created and initialized copy B(A); // Copy constructor called copy C=A; // Copy constructor called copy D; D=A; //copy constructor not called because object D not newly created object. //it is an assignment operation. return 0; }

In the code above, I had explicitly declared a constructor “copy( copy &c )”. This copy constructor is being called when object B is initialized using object A. Second time it is called when object C is being initialized using object A. When object D is initialized using object A the copy constructor is not called because when D is being initialized it is already in the existence, not the newly created one. Hence, here the assignment operator is invoked.

Definition of Assignment Operator

The assignment operator is an assigning operator of C++.  The “=” operator is used to invoke the assignment operator. It copies the data in one object identically to another object. The assignment operator copies one object to another member-wise. If you do not overload the assignment operator, it performs the bitwise copy. Therefore, you need to overload the assignment operator.

class copy{ int num; public: copy(){ } //default constructor copy(int a){ //initializing constructor num=a; } void show( ){ cout<< num; } }; int main(){ copy A(200); //Object A created and initialized copy B(300); // Object B created and initialized B=A; // assignment operator invoked copy C; C=A; // assignment operator invoked return 0; }

In above code when objectA is assigned to object B the assignment operator is being invoked as both the objects are already in existence. Similarly, same is the case when object C is initialized with object A.

When the bitwise assignment is performed both the object shares the same memory location and changes in one object reflect in another object.

Key Differences Between Copy Constructor and Assignment Operator

  1. A copy constructor is an overloaded contructor where as an assignment operator is a bitwise operator.
  2. Using copy constructor you can initialize a new object with an already existing object. On the other hand, an assignment operator copies one object to the other object, both of which are already in existance.
  3. A copy construcor is initialized whenever a new object is initialized with an already existing object, when an object is passed to a function as a non refrence parameter, or when an object is returned from a function. On the other hand, an assignment operator is invoked only when an object is being assigned to another object.
  4. When an object is being initialized using copy constructor, the initializing object and the initialized object shares the different memory location. On the other hand, when an object is being initialized using an assignment operator then the initialized and initializing objects shares the same memory location.
  5. If you do not explicitly define a copy constructor then the compiler provides one. On the other hand, if you do not overload an assigment operator then a bitwise copy operation is performed.

Conclusion:

The Copy constructor is best for copying one object to another when the object contains raw pointers.

Filed Under: Programming

One thought on “Copy Constructor Vs Assignment Operator Definition

Leave a comment

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *