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 the same memory location to the newly created target object as well as the source object.
Let us study the difference between the copy constructor and assignment operator.
Content: Copy Constructor Vs Assignment Operator
Comparison Chart
Basis for Comparison | Copy Constructor | Assignment Operator |
---|---|---|
Basic | The copy constructor is an overloaded constructor. | The assignment operator is a bitwise operator. |
Meaning | The 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. |
Syntax | class_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 Allocation | Both the target object and the initializing object shares the different memory locations. | Both the target object and the initializing object shares same allocated memory. |
Default | If 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 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 a 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 an 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 object A 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
- A copy constructor is an overloaded constructor whereas an assignment operator is a bitwise operator.
- 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 existence.
- A copy constructor is initialized whenever a new object is initialized with an already existing object, when an object is passed to a function as a non-reference 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.
- 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 share the same memory location.
- If you do not explicitly define a copy constructor then the compiler provides one. On the other hand, if you do not overload an assignment 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.
Leave a Reply