
- 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
When should we write our own assignment operator in C++?
Here we will see when we need to create own assignment operator in C++. If a class do not have any pointers, then we do not need to create assignment operator and copy constructor. C++ compiler creates copy constructor and assignment operator for each class. If the operators are not sufficient, then we have to create our own assignment operator.
Example
#include<iostream> using namespace std; class MyClass { //no user defined assignment operator or copy constructor is present int *ptr; public: MyClass (int x = 0) { ptr = new int(x); } void setValue (int x) { *ptr = x; } void print() { cout << *ptr << endl; } }; main() { MyClass ob1(50); MyClass ob2; ob2 = ob1; ob1.setValue(100); ob2.print(); }
Output
100
In the main() function, we have set the value of x using setValue() method for ob1. The value is also reflected in object ob2; This type of unexpected changes may generate some problems. There is no user defined assignment operator, so compiler creates one. Here it copies the ptr of RHS to LHS. So both of the pointers are pointing at the same location.
To solve this problem, we can follow two methods. Either we can create dummy private assignment operator to restrict object copy, otherwise create our own assignment operator.
Example
#include<iostream> using namespace std; class MyClass { //no user defined assignment operator or copy constructor is present int *ptr; public: MyClass (int x = 0) { ptr = new int(x); } void setValue (int x) { *ptr = x; } void print() { cout << *ptr << endl; } MyClass &operator=(const MyClass &ob2) { // Check for self assignment if(this != &ob2) *ptr = *(ob2.ptr); return *this; } }; main() { MyClass ob1(50); MyClass ob2; ob2 = ob1; ob1.setValue(100); ob2.print(); }
Output
50
Here the assignment operator is used to create deep copy and store separate pointer. One thing we have to keep in mind. We have to check the self-reference otherwise the assignment operator may change the value of current object.
- Related Articles
- When should we write our own assignment operator in C++ programming?
- Does C++ compiler create default constructor when we write our own?
- How can we create our own choice MySQL database?
- Java Assignment Operator Examples
- What is Assignment Operator (=) in JavaScript?
- What is Multiplication Assignment Operator (*=) in JavaScript?
- What is Addition Assignment Operator (+=) in JavaScript?
- What is an assignment operator in C#?
- Copy constructor vs assignment operator in C++
- Why do we close our eyes when we sneeze?
- What is Bitwise OR Assignment Operator (|=) in JavaScript?
- What is Bitwise XOR Assignment Operator (^=) in JavaScript?
- How to use an assignment operator in C#?
- When to use new operator in C++ and when it should not be used?
- Why should we not put a pin or pencil in our ears?
