Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Types and Language Concepts

Operator overloading in C++

Reference for C++ operator overloading: which operators can be overloaded, member versus free functions, canonical forms, stream operators and common pitfalls.

Operator overloading lets you give the built-in operators a meaning for your own types. An overloaded operator is an ordinary function with a special name, such as operator+ or operator<<. The expression a + b is translated into a call to operator+, either as a.operator+(b) or as operator+(a, b).

You cannot invent new operators, change precedence or arity, or redefine operators for built-in types only. The goal is to make code read naturally: Vec2 c = a + b; is good, a + b that sends a network request is not.

C++
struct Vec2 {
    double x, y;
    Vec2& operator+=(const Vec2& o) { x += o.x; y += o.y; return *this; }
};
Vec2 operator+(Vec2 a, const Vec2& b) { return a += b; }

Try it

C++ A small vector type

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Subscript, call, increment and stream input

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
R operator@(Params);              // free function; @ stands for the operator
R C::operator@(Params);           // member function: the left operand is *this
Group Operators and the usual form
Arithmetic + - * / % as free functions taking the left operand by value; implement them with the matching compound assignment.
Compound assignment += -= *= /= %= &= |= ^= <<= >>= as members, returning C& (*this).
Comparison == and <=> (C++20) as defaulted members where possible; the compiler derives !=, <, >, <=, >=.
Stream << and >> as free functions with std::ostream& / std::istream& on the left; return the stream.
Subscript, call, ->, = Must be members. operator[] may take several arguments since C++23.
Increment, decrement Prefix operator++() returns C&; postfix operator++(int) returns the old value by value. The int is a dummy marker.
Unary + - ! ~ * & : members with no parameters.

Cannot be overloaded: ::, ., .*, ?:, sizeof, typeid, and the cast keywords. &&, || and , can be, but you lose short-circuit evaluation and sequencing, so do not.

Usage notes

  • Member or free? =, [], (), -> and conversion operators must be members. Make binary operators that treat both operands equally free functions, so that implicit conversions apply to the left operand too (2 * v needs a free operator*(double, Vec2)).
  • Keep the semantics expected. a += b modifies a and returns a reference; a + b returns a new object and changes neither.
  • Define == and let != follow. Since C++20 the compiler rewrites a != b as !(a == b), and = default generates member-wise ==.
  • Stream operators are never members of your class, because the left operand is the stream. They often need a friend declaration or a public accessor.
  • Return types matter. Return C& from assignment and compound assignment, a new C from arithmetic, bool from comparisons.
  • Self-assignment: copy-assignment must survive a = a. Copy-and-swap is the easy way.
  • User-defined literals (operator""_km) are a separate form of overloading.
  • Overloading needs at least one user-defined type among the operands (a class or an enum); you cannot change int + int.
  • Evaluation order follows the built-in operator. a() + b() does not promise which call runs first, even with overloaded operators. Since C++17 the operands of <<, >>, = and the compound assignments are sequenced in a fixed order, with overloaded operators too.