C++ Operator Overloading
In this page:
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 <iostream>
class Vector2D {
public:
int x, y;
Vector2D operator-() {
return Vector2D{-x, -y};
}
};
int main() {
Vector2D v{3, 4};
Vector2D neg = -v;
std::cout << neg.x << "," << neg.y << std::endl;
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 <iostream>
class Point {
public:
int x, y;
Point operator+(const Point &other) {
return Point{x + other.x, y + other.y};
}
};
int main() {
Point p1{1, 2}, p2{3, 4};
Point sum = p1 + p2;
std::cout << sum.x << "," << sum.y << std::endl;
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 <iostream>
class Point {
public:
int x;
bool operator==(const Point &other) {
return x == other.x;
}
};
int main() {
Point p1{5}, p2{5};
std::cout << (p1 == p2) << std::endl;
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 <iostream>
class Point {
public:
int x;
Point(int x) : x(x) {}
friend Point operator+(int value, const Point &p);
};
Point operator+(int value, const Point &p) {
return Point(value + p.x);
}
int main() {
Point p(5);
Point result = 10 + p;
std::cout << result.x << std::endl;
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 <iostream>
class Multiplier {
public:
int factor;
int operator()(int x) {
return x * factor;
}
};
int main() {
Multiplier triple{3};
std::cout << triple(5) << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: