Operator overloading defines what an operator does when used with a class. An overloaded operator is a function named operator followed by a symbol:
class Point {
public:
Point(double px, double py) : x(px), y(py) {}
Point operator+(const Point& other) const {
return Point(x + other.x, y + other.y);
}
double x;
double y;
};
Point a(1, 2);
Point b(3, 4);
Point c = a + b; // Calls a.operator+(b), so c is (4, 6).An operator can be defined as a member, as a non-member function, or as a non-member friend declared inside the class so it can access private fields. The table shows each way an operator can be defined for a class T, where @ is a placeholder for the operator in the operator column:
| Operator | Category | Arity | Usage | Possible Definitions |
|---|---|---|---|---|
+, -, *, /, % | Arithmetic | Binary | a + b, a - b, a * b, a / b, a % b | T T::operator@(const T& b) constT operator@(const T& a, const T& b)friend T operator@(const T& a, const T& b) |
+, - | Arithmetic | Unary | +a, -a | T T::operator@() constT operator@(const T& a)friend T operator@(const T& a) |
&, |, ^, <<, >> | Bitwise | Binary | a & b, a | b, a ^ b, a << b, a >> b | T T::operator@(const T& b) constT operator@(const T& a, const T& b)friend T operator@(const T& a, const T& b) |
~ | Bitwise | Unary | ~a | T T::operator~() constT operator~(const T& a)friend T operator~(const T& a) |
==, !=, <, >, <=, >= | Comparison | Binary | a == b, a != b, a < b, a > b, a <= b, a >= b | bool T::operator@(const T& b) constbool operator@(const T& a, const T& b)friend bool operator@(const T& a, const T& b) |
&&, || | Logical | Binary | a && b, a || b | bool T::operator@(const T& b) constbool operator@(const T& a, const T& b)friend bool operator@(const T& a, const T& b) |
! | Logical | Unary | !a | bool T::operator!() constbool operator!(const T& a)friend bool operator!(const T& a) |
<< | I/O | Binary | os << a | std::ostream#include <iostream>& operator<<(std::ostream#include <iostream>& os, const T& a)friend std::ostream#include <iostream>& operator<<(std::ostream#include <iostream>& os, const T& a) |
>> | I/O | Binary | is >> a | std::istream#include <iostream>& operator>>(std::istream#include <iostream>& is, T& a)friend std::istream#include <iostream>& operator>>(std::istream#include <iostream>& is, T& a) |
= | Assignment | Binary | a = b | T& T::operator=(const T& b) |
+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>= | Compound assignment | Binary | a += b, a -= b, a *= b, a /= b, a %= b, a &= b, a |= b, a ^= b, a <<= b, a >>= b | T& T::operator@(const T& b)T& operator@(T& a, const T& b)friend T& operator@(T& a, const T& b) |
++, -- | Increment and decrement | Unary | ++a, --a | T& T::operator@()T& operator@(T& a)friend T& operator@(T& a) |
++, -- | Increment and decrement | Unary | a++, a-- | T T::operator@(int)T operator@(T& a, int)friend T operator@(T& a, int) |
[] | Subscript | Binary | a[i] | R& T::operator[](std::size_t#include <cstddef> i)const R& T::operator[](std::size_t#include <cstddef> i) const |
() | Function call | Any | a(x, y) | R T::operator()(X x, Y y) |
-> | Member access | Unary | a->m | R* T::operator->() |
* | Indirection | Unary | *a | R& T::operator*() constR& operator*(const T& a)friend R& operator*(const T& a) |
->* | Member access | Binary | a->*m | R& T::operator->*(M m)R& operator->*(const T& a, M m)friend R& operator->*(const T& a, M m) |
& | Address-of | Unary | &a | R* T::operator&()R* operator&(T& a)friend R* operator&(T& a) |
, | Comma | Binary | a, b | R T::operator,(const T& b)R operator,(const T& a, const T& b)friend R operator,(const T& a, const T& b) |
U | Conversion | Unary | U u = a, (U)a | T::operator U() const |
new, new[] | Allocation | new T, new T[n] | static void* T::operator new(std::size_t#include <cstddef> n)static void* T::operator new[](std::size_t#include <cstddef> n)void* operator new(std::size_t#include <cstddef> n)void* operator new[](std::size_t#include <cstddef> n) | |
delete, delete[] | Deallocation | delete p, delete[] p | static void T::operator delete(void* p)static void T::operator delete[](void* p)void operator delete(void* p)void operator delete[](void* p) |
Note
Overloaded &&, || and , evaluate both operands, so they lose the short-circuiting and ordering of the built-in versions. Avoid overloading them.
| Operator | Category | Arity | Usage | Definition |
|---|---|---|---|---|
U | Conversion | Unary | if (a), static_cast<U>(a) | explicit T::operator U() const |
"" | User-defined literal | 5_km | T operator"" _km(unsigned long long n) | |
"" | User-defined literal | 1.5_km | T operator"" _km(long double n) | |
"" | User-defined literal | "abc"_s | T operator"" _s(const char* s, std::size_t#include <cstddef> n) |
| Operator | Category | Arity | Usage | Definition |
|---|---|---|---|---|
==, != | Comparison | Binary | a == b, a != b | bool T::operator==(const T& b) const = defaultfriend bool operator==(const T& a, const T& b) = default |
<=>, <, >, <=, >= | Three-way comparison | Binary | a <=> b, a < b, a > b, a <= b, a >= b | auto T::operator<=>(const T& b) const = defaultfriend auto operator<=>(const T& a, const T& b) = defaultstd::strong_ordering#include <compare> T::operator<=>(const T& b) conststd::strong_ordering#include <compare> operator<=>(const T& a, const T& b)friend std::strong_ordering#include <compare> operator<=>(const T& a, const T& b) |
co_await | Coroutine | Unary | co_await a | R T::operator co_await()R operator co_await(T& a)friend R operator co_await(T& a) |
A defaulted == compares every field in order, and generates !=.
A defaulted <=> compares every field field in order and generates <, <=, > and >=.
| Operator | Category | Arity | Usage | Definition |
|---|---|---|---|---|
[] | Subscript | Any | a[i, j] | R& T::operator[](std::size_t#include <cstddef> i, std::size_t#include <cstddef> j) |
[] | Subscript | Any | a[i] | static R& operator[](std::size_t#include <cstddef> i) |
[] | Subscript | Binary | a[i] | template <typename Self> auto& T::operator[](this Self&& self, std::size_t#include <cstddef> i) |
() | Function call | Any | a(x, y) | static R operator()(X x, Y y) |
Dan