20 Apr 2017

Difference Between Copy Constructor and Assignment Operator in C++


Copy-constructor-assignment-operatorCopy 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
  1. class_ name (class_name &object_name){
  2. .
  3. // body of copy constructor
  4. .
  5. } // 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.
  1. class copy{
  2. int num;
  3. public:
  4. copy(){ } //default constructor
  5. copy(int a){ //initializing constructor
  6. num=a;
  7. }
  8. copy( copy &c ){ //Copy constructor
  9. num = c.num;
  10. }
  11. void show( ){
  12. cout<< num;
  13. }
  14. };
  15. int main(){
  16. copy A(200); //Object A created and initialized
  17. copy B(A); // Copy constructor called
  18. copy C=A; // Copy constructor called
  19. copy D;
  20. D=A; //copy constructor not called because object D not newly created object.
  21. //it is an assignment operation.
  22. return 0;
  23. }
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.
  1. class copy{
  2. int num;
  3. public:
  4. copy(){ } //default constructor
  5. copy(int a){ //initializing constructor
  6. num=a;
  7. }
  8. void show( ){
  9. cout<< num;
  10. }
  11. };
  12. int main(){
  13. copy A(200); //Object A created and initialized
  14. copy B(300); // Object B created and initialized
  15. B=A; // assignment operator invoked
  16. copy C;
  17. C=A; // assignment operator invoked
  18. return 0;
  19. }
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.

No comments:

Post a Comment

commnet here