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.
SomeType
can 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
Score
class stores a privatepoints
value. - Two separate overloads of the
+
operator are defined to handle different operand orders. - Each overload is declared as a
friend
to allow direct access to private data. - This approach ensures that both
Score + value
andvalue + Score
expressions 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.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C++
Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.Beginner Friendly11 hours