When should we write our own assignment operator in C++? Last Updated : 28 Sep, 2018 Comments Improve Suggest changes Like Article Like Report The answer is same as Copy Constructor. If a class doesn't contain pointers, then there is no need to write assignment operator and copy constructor. The compiler creates a default copy constructor and assignment operators for every class. The compiler created copy constructor and assignment operator may not be sufficient when we have pointers or any run time allocation of resource like file handle, a network connection..etc. For example, consider the following program. CPP #include<iostream> using namespace std; // A class without user defined assignment operator class Test { int *ptr; public: Test (int i = 0) { ptr = new int(i); } void setValue (int i) { *ptr = i; } void print() { cout << *ptr << endl; } }; int main() { Test t1(5); Test t2; t2 = t1; t1.setValue(10); t2.print(); return 0; } Output of above program is "10". If we take a look at main(), we modified 't1' using setValue() function, but the changes are also reflected in object 't2'. This type of unexpected changes cause problems. Since there is no user defined assignment operator in the above program, compiler creates a default assignment operator, which copies 'ptr' of right hand side to left hand side. So both 'ptr's start pointing to the same location. We can handle the above problem in two ways. 1) Do not allow assignment of one object to other object. We can create our own dummy assignment operator and make it private. 2) Write your own assignment operator that does deep copy. Same is true for Copy Constructor. Following is an example of overloading assignment operator for the above class. CPP #include<iostream> using namespace std; class Test { int *ptr; public: Test (int i = 0) { ptr = new int(i); } void setValue (int i) { *ptr = i; } void print() { cout << *ptr << endl; } Test & operator = (const Test &t); }; Test & Test::operator = (const Test &t) { // Check for self assignment if(this != &t) *ptr = *(t.ptr); return *this; } int main() { Test t1(5); Test t2; t2 = t1; t1.setValue(10); t2.print(); return 0; } Output5 We should also add a copy constructor to the above class, so that the statements like "Test t3 = t4;" also don't cause any problem. Note the if condition in assignment operator. While overloading assignment operator, we must check for self assignment. Otherwise assigning an object to itself may lead to unexpected results (See this). Self assignment check is not necessary for the above 'Test' class, because 'ptr' always points to one integer and we may reuse the same memory. But in general, it is a recommended practice to do self-assignment check. Comment More infoAdvertise with us Next Article When should we write our own assignment operator in C++? kartik Follow Improve Article Tags : C Language C++ Practice Tags : CPP Similar Reads When Should We Write Our Own Copy Constructor in C++? A copy constructor is a member function that initializes an object using another object of the same class. (See this article for reference).When should we write our own copy constructor?C++ compiler provides a default copy constructor (and assignment operator) with class. When we don't provide an im 2 min read Assignment Operators in C++ In C++, the assignment operator forms the backbone of computational processes by performing a simple operation like assigning a value to a variable. It is denoted by equal sign ( = ) and provides one of the most basic operations in any programming language i.e. assign some value to the variables in 6 min read Assignment Operators in C In C, assignment operators are used to assign values to variables. The left operand is the variable and the right operand is the value being assigned. The value on the right must match the data type of the variable otherwise, the compiler will raise an error.Let's take a look at an example:C#include 4 min read C++ Assignment Operator Overloading Prerequisite: Operator OverloadingThe assignment operator,"=", is the operator used for Assignment. It copies the right value into the left value. Assignment Operators are predefined to operate only on built-in Data types.Assignment operator overloading is binary operator overloading.Overloading ass 4 min read Self assignment check in assignment operator In C++, assignment operator should be overloaded with self assignment check. For example, consider the following class Array and overloaded assignment operator function without self assignment check. C // A sample class class Array { private: int *ptr; int size; public: Array& operator = (const 2 min read Copy Constructor vs Assignment Operator in C++ Copy constructor and Assignment operator are similar as they are both used to initialize one object using another object. But, there are some basic differences between them:Copy constructor Assignment operator It is called when a new object is created from an existing object, as a copy of the existi 2 min read Operator Precedence and Associativity in C++ In C++,operator precedence and associativity are important concepts that determine the order in which operators are evaluated in an expression. Operator precedence tells the priority of operators, while associativity determines the order of evaluation when multiple operators of the same precedence l 3 min read Scope Resolution Operator in C++ In C++, the scope resolution operator (::) is used to access the identifiers such as variable names and function names defined inside some other scope in the current scope. Let's take a look at an example:C++#include <iostream> int main() { // Accessing cout from std namespace using scope // r 4 min read Written version of Logical operators in C++ Can we use keywords in place of operators in C++ ? Yes, certainly, we can. The ANSI C++ Standard has proposed keywords for several C++ operators . They originated in C in the header at the time when there were keyboards that couldn't type the required symbols like &&, !, || etc. In C++, they became 2 min read unordered_multimap operator= in C++ The unordered_multimap::operator= is a built-in function in C++ STL which does three types of tasks which are explained below. Syntax (copying elements from different container) : unordered_multimap_name1 operator= (unordered_multimap_name2)Parameters: The function does not accepts any parameter. Th 4 min read Like