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:

OperatorCategoryArityUsagePossible Definitions
+, -, *, /, %ArithmeticBinarya + b, a - b, a * b, a / b, a % bT T::operator@(const T& b) const

T operator@(const T& a, const T& b)

friend T operator@(const T& a, const T& b)
+, -ArithmeticUnary+a, -aT T::operator@() const

T operator@(const T& a)

friend T operator@(const T& a)
&, |, ^, <<, >>BitwiseBinarya & b, a | b, a ^ b, a << b, a >> bT T::operator@(const T& b) const

T operator@(const T& a, const T& b)

friend T operator@(const T& a, const T& b)
~BitwiseUnary~aT T::operator~() const

T operator~(const T& a)

friend T operator~(const T& a)
==, !=, <, >, <=, >=ComparisonBinarya == b, a != b, a < b, a > b, a <= b, a >= bbool T::operator@(const T& b) const

bool operator@(const T& a, const T& b)

friend bool operator@(const T& a, const T& b)
&&, ||LogicalBinarya && b, a || bbool T::operator@(const T& b) const

bool operator@(const T& a, const T& b)

friend bool operator@(const T& a, const T& b)
!LogicalUnary!abool T::operator!() const

bool operator!(const T& a)

friend bool operator!(const T& a)
<<I/OBinaryos << astd::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/OBinaryis >> astd::istream#include <iostream>& operator>>(std::istream#include <iostream>& is, T& a)

friend std::istream#include <iostream>& operator>>(std::istream#include <iostream>& is, T& a)
=AssignmentBinarya = bT& T::operator=(const T& b)
+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=Compound assignmentBinarya += b, a -= b, a *= b, a /= b, a %= b, a &= b, a |= b, a ^= b, a <<= b, a >>= bT& T::operator@(const T& b)

T& operator@(T& a, const T& b)

friend T& operator@(T& a, const T& b)
++, --Increment and decrementUnary++a, --aT& T::operator@()

T& operator@(T& a)

friend T& operator@(T& a)
++, --Increment and decrementUnarya++, a--T T::operator@(int)

T operator@(T& a, int)

friend T operator@(T& a, int)
[]SubscriptBinarya[i]R& T::operator[](std::size_t#include <cstddef> i)

const R& T::operator[](std::size_t#include <cstddef> i) const
()Function callAnya(x, y)R T::operator()(X x, Y y)
->Member accessUnarya->mR* T::operator->()
*IndirectionUnary*aR& T::operator*() const

R& operator*(const T& a)

friend R& operator*(const T& a)
->*Member accessBinarya->*mR& T::operator->*(M m)

R& operator->*(const T& a, M m)

friend R& operator->*(const T& a, M m)
&Address-ofUnary&aR* T::operator&()

R* operator&(T& a)

friend R* operator&(T& a)
,CommaBinarya, bR T::operator,(const T& b)

R operator,(const T& a, const T& b)

friend R operator,(const T& a, const T& b)
UConversionUnaryU u = a, (U)aT::operator U() const
new, new[]Allocationnew 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[]Deallocationdelete p, delete[] pstatic 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.

OperatorCategoryArityUsageDefinition
UConversionUnaryif (a), static_cast<U>(a)explicit T::operator U() const
""User-defined literal5_kmT operator"" _km(unsigned long long n)
""User-defined literal1.5_kmT operator"" _km(long double n)
""User-defined literal"abc"_sT operator"" _s(const char* s, std::size_t#include <cstddef> n)
OperatorCategoryArityUsageDefinition
==, !=ComparisonBinarya == b, a != bbool T::operator==(const T& b) const = default

friend bool operator==(const T& a, const T& b) = default
<=>, <, >, <=, >=Three-way comparisonBinarya <=> b, a < b, a > b, a <= b, a >= bauto T::operator<=>(const T& b) const = default

friend auto operator<=>(const T& a, const T& b) = default

std::strong_ordering#include <compare> T::operator<=>(const T& b) const

std::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_awaitCoroutineUnaryco_await aR 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 >=.

OperatorCategoryArityUsageDefinition
[]SubscriptAnya[i, j]R& T::operator[](std::size_t#include <cstddef> i, std::size_t#include <cstddef> j)
[]SubscriptAnya[i]static R& operator[](std::size_t#include <cstddef> i)
[]SubscriptBinarya[i]template <typename Self> auto& T::operator[](this Self&& self, std::size_t#include <cstddef> i)
()Function callAnya(x, y)static R operator()(X x, Y y)

References