
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Overloading Unary Plus Operator in C++
The unary operators are operators that operate only on a single operand. There are mainly thirteen unary operators in C++, for example, ++, !, ~, typeof, delete, etc.
Overloading a unary operator means setting a customized behaviour for the unary operators for the objects of a class. which means you can define how an operator will work when applied to instances of a class instead of using its default behavior. This operator is generally used on the left side of the object, as in +obj, !obj, -obj, and ++obj, but can also be used as a postfix like obj++ or obj--.
So, overloading the unary + operator defines the customized behaviour of the + operator. But overloading unary + doesn't bring any significant change to the value of the object (as the + sign is just used to indicate the positive value), but you can use it for cleaner and more expressive code and to prevent any accidental modification of an object.
Syntax
Here is the following syntax for overloading a unary plus (+) operator in C++.
Member Function Syntax:
return_type operator+();
Friend Function Syntax:
A friend function (Non-Member Function) is a function that is defined outside the class but also has access to the class's private members.
The Friends function is used if the user wants to access private members.
friend return_type operator+ (const ClassName& obj);
Example
Here is the following example code showcasing the operator overloading in a friend function.
#include <iostream> using namespace std; class Number { public: int value; Number(int v) { value = v; } // Overload unary + operator Number operator+() { return *this; // returns the same object } }; int main() { Number num(5); Number result = +num; // Calls overloaded + cout << "Value: " << result.value << endl; return 0; }
Output
Value: 5
Explanation
- In this, first, a class Number is defined with a member value and then overloaded unary + operator using operator+() function, which simply returns the current object (*this) without modifying it.
- Now, in the main(), the overloaded operator is called, which returns a copy of the num object.
- This copy is assigned to result, and then result.value is printed, which is 5
Use case of Overloading Unary Plus Operator?
Though overloading the unary plus operator (+) doesn't bring any significant changes, it simply returns the value as it is, but can be used to make your code more expressive, help debugging, and reset internal values.
Here are the following use cases of it.
- It is used for creating copies or cloning of an object.
- It is used to return a normalized version of your object.
- Developers also used it for unit testing or debugging, like overloading to log object state or track operations internally while debugging.