
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
What's the difference between assignment operator and copy constructor in C++?
The Copy constructor and the assignment operators are used to initialize one object to another object. The main difference between them is that the copy constructor creates a separate memory block for the new object. But the assignment operator does not make new memory space. It uses reference variable to point to the previous memory block.
Copy Constructor (Syntax)
classname (const classname &obj) { // body of constructor }
Assignment Operator (Syntax)
classname Ob1, Ob2; Ob2 = Ob1;
Let us see the detailed differences between Copy constructor and Assignment Operator.
Copy Constructor | Assignment Operator |
---|---|
The Copy constructor is basically an overloaded constructor | Assignment operator is basically an operator. |
This initializes the new object with an already existing object | This assigns the value of one object to another object both of which are already exists. |
Copy constructor is used when a new object is created with some existing object | This operator is used when we want to assign existing object to new object. |
Both the objects uses separate memory locations. | One memory location is used but different reference variables are pointing to the same location. |
If no copy constructor is defined in the class, the compiler provides one. | If the assignment operator is not overloaded then bitwise copy will be made |
- Related Articles
- Difference Between Copy Constructor and Assignment Operator in C++
- Copy constructor vs assignment operator in C++
- What is the difference between new operator and object() constructor in JavaScript?
- What is the difference between = and: = assignment operators?
- What is the difference between shallow copy and deep copy in Java?
- What is Assignment Operator (=) in JavaScript?
- What is the difference between initialization and assignment of values in C#?
- Difference between Static Constructor and Instance Constructor in C#
- What is the meaning and usage of ‘=&’ assignment operator in PHP?
- What is Multiplication Assignment Operator (*=) in JavaScript?
- What is Addition Assignment Operator (+=) in JavaScript?
- What is an assignment operator in C#?
- Difference Between Constructor and Destructor
- What is Bitwise OR Assignment Operator (|=) in JavaScript?
- What is Bitwise XOR Assignment Operator (^=) in JavaScript?

Advertisements