C++ Operator Overloading
In this page:
class ClassName {
public:
ClassName operator-(); // unary
ClassName operator+(const ClassName &other); // binary
};
Overloading Unary Operators
C++ lets you redefine how unary operators like '-' or '!' behave when applied to your own class's objects.
A unary operator acts on a single operand, so overloading it lets expressions like -myVector or !isEmpty read naturally for user-defined types too.
Example: Overloading Unary Operators
// Include std::cout and std::cin
#include <iostream>
// Define the type Vector2D
class Vector2D {
// Members below can be accessed from outside the class
public:
int x, y;
Vector2D operator-() {
// Send Vector2D{-x, -y} back to the caller
return Vector2D{-x, -y};
}
};
// Program execution starts in main()
int main() {
Vector2D v{3, 4};
Vector2D neg = -v;
// Print to the console with cout
std::cout << neg.x << "," << neg.y << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Overloading Binary Operators
Binary operators act on two operands, and overloading them — such as '+' or '-' — lets you write expressions like point1 + point2 that combine two objects of your class the same way you'd combine two built-in numbers, instead of calling an awkwardly-named method.
Example: Overloading Binary Operators
// Include std::cout and std::cin
#include <iostream>
// Define the type Point
class Point {
// Members below can be accessed from outside the class
public:
int x, y;
Point operator+(const Point &other) {
return Point{x + other.x, y + other.y};
}
};
// Program execution starts in main()
int main() {
Point p1{1, 2}, p2{3, 4};
Point sum = p1 + p2;
// Print to the console with cout
std::cout << sum.x << "," << sum.y << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Overloading Relational Operators
Overloading relational operators like '==' or '<' lets you compare your own objects directly inside if-statements or use them with STL algorithms like sort, which rely on '<' to determine ordering between elements.
Example: Overloading Relational Operators
// Include std::cout and std::cin
#include <iostream>
// Define the type Point
class Point {
// Members below can be accessed from outside the class
public:
int x;
bool operator==(const Point &other) {
// Send x == other.x back to the caller
return x == other.x;
}
};
// Program execution starts in main()
int main() {
Point p1{5}, p2{5};
// Print to the console with cout
std::cout << (p1 == p2) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Overloading with Friend Functions
Some operator overloads are written as non-member friend functions rather than member functions — this is required whenever the left-hand operand isn't an object of your class, such as when overloading '<<' for std::cout or when you want symmetric behavior on both sides of '+'.
Example: Overloading with Friend Functions
// Include std::cout and std::cin
#include <iostream>
// Define the type Point
class Point {
// Members below can be accessed from outside the class
public:
int x;
Point(int x) : x(x) {}
friend Point operator+(int value, const Point &p);
};
Point operator+(int value, const Point &p) {
// Send Point(value + p.x) back to the caller
return Point(value + p.x);
}
// Program execution starts in main()
int main() {
Point p(5);
Point result = 10 + p;
// Print to the console with cout
std::cout << result.x << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Special Overloaded Operators
C++ also allows overloading special operators with unusual roles: the assignment operator '=' controls what happens on copy-assignment, and the function-call operator '()' lets an object be called like a function — the mechanism behind functors used throughout the STL.
Example: Special Overloaded Operators
// Include std::cout and std::cin
#include <iostream>
// Define the type Multiplier
class Multiplier {
// Members below can be accessed from outside the class
public:
int factor;
int operator()(int x) {
// Send x * factor back to the caller
return x * factor;
}
};
// Program execution starts in main()
int main() {
Multiplier triple{3};
// Print to the console with cout
std::cout << triple(5) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Overloading an operator to do something unexpected.
- Trying to overload operators like
::or., which cannot be overloaded. - Returning by value when a reference is needed, such as in
operator=.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: