1. About Assignment Operator Overloading
By default, any class will have a compiler given copy constructor and assignment operator. In order to avoid shallow copy one will overload the copy constructor as well as Assignment Operator. The below picture shows the syntax for the ‘= Operator’ overloading:

Assignment Operator Overloading Syntax
The left-hand side of the assignment operator will make a call to the Operator =. Hence, it will pass the value on the right-hand side as the argument. The overloading function gets the argument as a const reference. This will guarantee that the right side operand never changes. In this article, we will see how to overload an assignment operator.
2. CPoint3d Class
Below are the private members of the CPoint3d class which we will experiment using the overloaded assignment operator.
1 2 3 4 5 |
//Sample 01: Private Members private: int m_x; int m_y; int m_z; |
This class
CPoint3d
is initialized through two kinds of the constructor. One is a custom-defined default constructor, which sets all the data members of the
CPoint3d
class as zero. The other one takes all required values as arguments and set the internal members. The below code shows both the constructors below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//Sample 02: Constructor CPoint3d() { m_x = 0; m_y = 0; m_z = 0; } //Sample 03: Constructor CPoint3d(int px, int py, int pz) { m_x = px; m_y = py; m_z = pz; } |
The class has a print method, which prints all the coordinate values on the screen. The function is shown below:
1 2 3 4 5 6 7 8 9 |
//Sample 04: Print Method void Print(char * name) { cout<<name<<endl; cout<<"X :" <<m_x <<endl; cout<<"Y :" <<m_y <<endl; cout<<"Z :" <<m_z <<endl; cout<<endl; } |
3. Overloading the Assignment Operator
The overloaded ‘operator =’ shown in the below picture:

Overloaded = Operator Example
In the above code, the left-hand side of the assignment calls overloaded
operator=
function. The right-hand side value is going as an argument to the function. The implementation copies the values from the passed-in argument to the calling object
p1
.
Note, the code has a conditional check,
this == &rhs
. This conditional check is useful to avoid the self-assignment. The keyword
this
gives the calling object’s address which is nothing but the address of the object on the left side. Since we received the argument to the operator overloading as a reference,
&rhs
in our case gives the address of the right-hand side object. So, using this address-check, we can avoid self-assignments. Returning the object is useful for making assignment as part of expressions as shown in the above picture. Below is the code (For copy paste):
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//Sample 05: Overloading Assignment operator (Copy is between two objects) CPoint3d& operator=(const CPoint3d& rhs) { if (this == &rhs) return *this; else { m_x = rhs.m_x; m_y = rhs.m_y; m_z = rhs.m_z; return *this; } } |
4. Supporting const as RValue
The implementation below shows how to handle the assignment for the constant values. In our case, the implementation supports constant values for integer. Say, for example, the below implementation makes the
P1 = 73
possible. It assigns all the
P1
coordinate values to 73.
1 2 3 4 5 6 |
//Sample 06: Overloading Assignment Operator (Copy is between Object and Constant CPoint3d& operator=(const int all) { m_x = m_y = m_z = all; return *this; } |
5. Testing the class
In the below example, we create two objects. We set some sample values to object
pt1
by invoking the constructor with three arguments. We set the object
pt2
with default values through constructor with no arguments. After setting up the object, we copy the content of the object
pt1
to
pt2
using our overloaded operator. The code is below:
1 2 3 4 5 6 7 |
//Sample 07: Basic Operation CPoint3d pt1(10,20,30); CPoint3d pt2; pt1.Print("pt1"); <strong>pt2 = pt1;</strong> pt1.Print("pt1"); pt2.Print("pt2"); |
In the next snippet, self-assignment and the multiple assignments are tested. Note, the conditional check against the this object avoids the self-assignment. In our example
pt3 = pt3
tests the self-assignment and the statement
pt1 = pt2 = pt3
tests the multi-assignment.
1 2 3 4 5 6 7 |
//Sample 08: Self and Multiple Assignment CPoint3d pt3(11,12,14); pt3 = pt3; pt1 = pt2 = pt3; pt1.Print("pt1"); pt2.Print("pt2"); pt3.Print("pt3"); |
In the final snippet, we check our second version of the overload. Recall, this version takes a number as right hand side value. This means the left side is an object, and it calls the operator. Right side of the operator is a number. It goes as an argument to the operator. Below is the code for it:
1 2 3 4 |
//Sample 09: Assignment of Constant value CPoint3d pt4; pt4 = 7; pt4.Print("pt4"); |
Below is the output of executing the full code:

Overloaded Operator Example
6. Complete Source Code Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
#include "stdafx.h" #include <iostream> using namespace std; //Sample 01: A class that denotes two numbers (For Example only) class CPoint3d { //Sample 01: Private Members private: int m_x; int m_y; int m_z; public: //Sample 02: Default Constructor CPoint3d() { m_x = 0; m_y = 0; m_z = 0; } //Sample 03: Constructor with Parameters CPoint3d(int px, int py, int pz) { m_x = px; m_y = py; m_z = pz; } //Sample 04: Print Method void Print(char * name) { cout<<name<<endl; cout<<"X :" <<m_x <<endl; cout<<"Y :" <<m_y <<endl; cout<<"Z :" <<m_z <<endl; cout<<endl; } //Sample 05: Overloading Assignment operator (Copy is between two objects) CPoint3d& operator=(const CPoint3d& rhs) { if (this == &rhs) return *this; else { m_x = rhs.m_x; m_y = rhs.m_y; m_z = rhs.m_z; return *this; } } //Sample 06: Overloading Assignment Operator (Copy is between Object and Constant CPoint3d& operator=(const int all) { m_x = m_y = m_z = all; return *this; } }; void main() { //Sample 07: Basic Operation CPoint3d pt1(10,20,30); CPoint3d pt2; pt1.Print("pt1"); pt2 = pt1; pt1.Print("pt1"); pt2.Print("pt2"); //Sample 08: Self and Multiple Assignment CPoint3d pt3(11,12,14); pt3 = pt3; pt1 = pt2 = pt3; pt1.Print("pt1"); pt2.Print("pt2"); pt3.Print("pt3"); //Sample 09: Assignment of Constant value CPoint3d pt4; pt4 = 7; pt4.Print("pt4"); } |
7. Other Articles
You can read other operator overloading examples here:
Categories: C++
Tags: Operator Overloading, Operator=, Overloaded =