1. Introduction
We know that a binary operator takes two operands in which it performs the operation. Say, for example, the addition operator adds two numbers. We call these two numbers as operands and the ‘+’ as a binary operator. Now let us deal with the number ‘-12’. Here the minus sign states the negation of the number. The negation operator operates on only one operand. So we can say the ‘-‘ as an Unary Operator. In this example, we will see how to overload the ++ Unary Operator in both prefix and postfix forms.
2. The Number Class for Prefix Overload and PostFix Overload
The class
TheNumber
shown below has two private integer members. The constructor with two integer parameter sets default values to the class private members. Print method of this class prints the private member values in the console output.
In the code below, we also overloaded the binary operator +. For more information on Binary operator overloading, read the article here: Binary Operator Overloading. The initial code for the class is below:
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 |
//Sample 01: A class that denotes two numbers (For Example only) class TheNumber { private: int m_Number1; int m_Number2; public: //Sample 02: Constructor TheNumber(int x, int y) { m_Number1 = x; m_Number2 = y; } //Sample 03: The print Method void print() { cout<<"m_Number1 ="<< m_Number1<<endl; cout<<"m_Number2 ="<<m_Number2<<endl; } //Sample 04: Overload Binary Operator + TheNumber operator+(TheNumber obj2) { int loc_Number1 = m_Number1 + obj2.m_Number1; int loc_Number2 = m_Number2 + obj2.m_Number2; return TheNumber(loc_Number1, loc_Number2); } }; |
3. Overload Prefix increment Operator ++
3.1 Prefix Overload Implementation
We can code the prefix operator the same way as we did for the binary + operator. Note that the implementation does not take any argument and the calling object itself serves the data for the first operand. Have a look at the below implementation:
1 2 3 4 5 6 7 |
//Sample 05: Overloaded Unary Prefix operator TheNumber operator++() { m_Number1 = m_Number1 + 1; m_Number2 = m_Number2 + 1; return *this; } |
In this implementation, we raise both the private members by adding one to it. After this, we return the current object to the caller. Remember that the current object is the only possible operand which called this function. With this overloaded operator, now it is possible to use the prefix notation of increment on an object. For example, if the instance is K, then we can use ++K, to increment both of its private members.
3.2 Overloaded Prefix Usage
Now have a look at the usage code shown below:
1 2 3 4 5 6 7 |
//Usage 01: The code that uses unary and binary operators TheNumber num1(1,1); TheNumber num2(1,1); //Usage 02: Use the prefix increment operator TheNumber num3 = ++num1 + ++num2; num3.print(); |
In the above case, we created two objects. They are
num1
and
num2
. The
usage02
code snippet applies the prefix operator on both the objects
num1
and
num2
before adding them together using operator +.
3.3 Calling Sequence
The calling sequence of Prefix Overload goes like this:
- First, C++ calls the prefix increment for num1
- Secondly, the prefix increment operator for the num2 object is called.
- Thirdly, the binary operator + is called on both the objects num1 and num2
At step 1, the private members for the
num1
object are incremented, and the object is returned. At step 2, the private members for the
num2
object are incremented and
num2
is returned. In the third step, we add the incremented objects using overloaded binary operator plus. So, we will see the result as shown below:

Prefix Operator Program Output
The below picture explains the above process:

Overloaded Prefix Operator Sequence.
4. Overloading Postfix ++ Operator
4.1 Postfix Overload Implementation
In C++, we should overload the postfix operator somewhat differently. This is to help the run-time to avoid ambiguity. First, the operator function takes a dummy integer argument to differentiate this from the prefix overloaded operator. Next, in the implementation we clone the object. Then do increment on the calling object i.e. at the operand one. Lastly, return the cloned object. This way, we see the increment taking place after the statement execution. Have a look at the below implementation:
1 2 3 4 5 6 7 8 |
//Sample 06: Overloaded Unary Postfix operator TheNumber operator++(int dummyNo) { TheNumber cloned = (*this); m_Number1 = m_Number1 + 1; m_Number2 = m_Number2 + 1; return cloned; } |
Note that we do the clone first. Then we do the increment on the local object. Finally, we return the cloned copy to the caller. So in caller perspective, the expression which contains the operator, gets only the cloned copy. After the ‘expression statement’ is done, we see the increment taking part in the operand. The parameter dummy is not doing anything here, right? This parameter is useful for the compiler to distinguish between prefix and postfix overload.
4.2 Overloaded Postfix Usage
Look at the below code that uses the postfix operator:
1 2 3 4 5 6 7 |
//Usage 03: The code that uses unary and binary operators TheNumber num1(1,1); TheNumber num2(1,1); //Usage 04: Use the postfix increment operator TheNumber num4 = num1++ + num2++; num4.print(); |
Here, postfix operator for
num1
is called first. Then postfix operator for the
num2
is called. But the postfix operator on both the cases returned a cloned copy. These copies are then added together to get a result in the object
num4
.
The overloaded operator returns the cloned copy. The binary plus operator acts on these clones. But, in the actual memory of
Num1
and
Num2
, our operator function raised the internal members by 1. So, when we move to the next statement, we see the changed internal members.

Postfix Operator Output
4.3 Calling Sequence
For more explanation, have a look at the below picture. In the postfix operator, we make the changes on the real objects. But, we return the cloned copy which was taken before the change was applied. The binary plus operator actually operates on the cloned copies and hence we get the members with a value of two in the result object
num4
.

Overloaded Postfix Operator Sequence
5. Complete 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 |
#include "stdafx.h" #include <iostream> using namespace std; //Sample 01: A class that denotes two numbers //(For Example only) class TheNumber { private: int m_Number1; int m_Number2; public: //Sample 02: Constructor TheNumber(int x, int y) { m_Number1 = x; m_Number2 = y; } //Sample 03: The print Method void print() { cout<<"m_Number1 ="<< m_Number1<<endl; cout<<"m_Number2 ="<<m_Number2<<endl; } //Sample 04: Overload Binary Operator + TheNumber operator+(TheNumber obj2) { int loc_Number1 = m_Number1 + obj2.m_Number1; int loc_Number2 = m_Number2 + obj2.m_Number2; return TheNumber(loc_Number1, loc_Number2); } //Sample 05: Overloaded Unary Prefix operator TheNumber operator++() { m_Number1 = m_Number1 + 1; m_Number2 = m_Number2 + 1; return *this; } //Sample 06: Overloaded Unary Postfix operator TheNumber operator++(int dummyNo) { TheNumber cloned = (*this); m_Number1 = m_Number1 + 1; m_Number2 = m_Number2 + 1; return cloned; } }; void main() { //Usage 01: The code that uses unary and binary operators TheNumber num1(1,1); TheNumber num2(1,1); //Usage 02: Use the prefix increment operator TheNumber num3 = ++num1 + ++num2; num3.print(); //Usage 03: The code that uses unary and binary operators TheNumber num1(1,1); TheNumber num2(1,1); //Usage 04: Use the postfix increment operator TheNumber num4 = num1++ + num2++; num4.print(); } |
Categories: C++
Tags: Operator Overloading, Overload ++ Operator, Unary Operator Overloading