C++ Separate Overload
In C++, each operator overload is treated as a separate function based on its parameter types and order. If the order of operands changes, the compiler expects a different function. Even when performing the same operation, C++ does not automatically reverse or convert the arguments.
This is common when working with binary operators (like +) between a class and a built-in type. For example, to support both SomeClass + someValue and someValue + SomeClass, two separate overloads must be written.
Syntax
Here’s a syntax of showing how to write two separate overloads for a binary operator when the order of operands can vary:
// Overload for: object + value
ClassName operator+(const ClassName& obj, SomeType value);
// Overload for: value + object
ClassName operator+(SomeType value, const ClassName& obj);
- The first overload allows expressions like
object + value. - The second overload supports the reverse order:
value + object. - These are treated as distinct functions in C++, so both must be defined separately if both usage orders are needed.
SomeTypecan be any type likeint,float, or even another class.
Example: Using the + operator in both operand orders
The following example demonstrates how to overload a binary operator in both operand orders for a class named Score. This supports operations like Score + int and int + Score:
#include <iostream>using namespace std;class Score {private:int points;public:Score(int p) : points(p) {}void display() const {cout << "Points: " << points << endl;}// Friend function for Score + intfriend Score operator+(const Score& s, int extra);// Friend function for int + Scorefriend Score operator+(int extra, const Score& s);};// Definition for Score + intScore operator+(const Score& s, int extra) {return Score(s.points + extra);}// Definition for int + ScoreScore operator+(int extra, const Score& s) {return Score(extra + s.points);}int main() {Score s1(50);Score result1 = s1 + 20;Score result2 = 30 + s1;result1.display();result2.display();return 0;}
The above example will result in the following output:
Points: 70Points: 80
In this example:
- The
Scoreclass stores a privatepointsvalue. - Two separate overloads of the
+operator are defined to handle different operand orders. - Each overload is declared as a
friendto allow direct access to private data. - This approach ensures that both
Score + valueandvalue + Scoreexpressions compile correctly, as C++ does not automatically reverse the operands.
Codebyte Example
The following codebyte example overloads the plus (+) operator using friend functions to support both Distance + value and value + Distance. It demonstrates how separate overloads are defined to handle different operand orders. Compile the code to see the output of both operations.
Learn C++ on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
- Beginner Friendly.11 hours