

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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?
- Difference Between Constructor and Destructor
- What is the difference between initialization and assignment of values in C#?
- Difference between Static Constructor and Instance Constructor in C#
- What is Assignment Operator (=) in JavaScript?
- Difference between the and$ operator in php
- Difference between constructor and method in Java
- What's the difference between sizeof and alignof?
- What's the difference between window.location and document.location?
- What's the difference between Matplotlib.pyplot and Matplotlib.figure?
- What is the difference between getter/setter methods and constructor in Java?
Advertisements