Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Keywords and Statements

The operator keyword

Reference for the C++ operator keyword: overloading arithmetic, comparison, subscript, call and stream operators, conversion functions, and which operators cannot be overloaded.

The operator keyword introduces a function that gives a built-in operator a meaning for your own types. a + b on two Vec2 values becomes a call to operator+(a, b) or a.operator+(b). You can also define conversion functions (operator double()) and, since C++11, user-defined literals (operator""_km).

Overloading changes what an operator does for a class or enum, never for built-in types alone, and it cannot change precedence, associativity or arity. Good overloads follow the behavior users expect from the built-in version.

C++
struct Vec2 { double x, y; };
Vec2 operator+(Vec2 a, Vec2 b) { return {a.x + b.x, a.y + b.y}; }

Try it

C++ Arithmetic, comparison and stream operators

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++ Call, subscript, conversion and increment operators

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++
ReturnType operator@(params);               // non-member, @ is the operator symbol
ReturnType Class::operator@(params) [cv];   // member
operator Type() [cv] [explicit];            // conversion function, no return type
ReturnType operator""_suffix(params);       // user-defined literal

The following can be overloaded: + - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= <=> && || ++ -- , ->* -> () [] new delete new[] delete[], plus co_await.

These cannot be overloaded: ::, ., .*, ?:, sizeof, typeid, alignof and the cast keywords.

Usage notes

  • =, (), [] and -> must be members. Stream operators << and >> are non-members because the left operand is the stream. Make other binary operators non-members (or hidden friends) so that implicit conversions apply to both operands equally.
  • Implement compound assignment (+=) as a member first, then write + in terms of it. Take the left operand by value in operator+ to get one free copy.
  • Prefix ++ returns a reference; postfix ++ takes a dummy int parameter and returns the old value. Postfix is more expensive, so prefer prefix in loops over iterators.
  • With C++20 you can default == and <=> (auto operator<=>(const T&) const = default;) and the compiler writes the other four comparisons. See the three-way comparison page.
  • C++23 allows operator[] to take any number of arguments, so m[row, col] works. C++23 also allows static operator() and static operator[].
  • Overloading &&, || and , loses short-circuit evaluation and the guaranteed evaluation order; avoid them. Overloading unary & is almost always a mistake.
  • Keep semantics natural: == should be an equivalence, + should not mutate its operands, << should return the stream.
  • Conversion functions should usually be explicit, except for tiny, lossless cases. Do not give a class both an implicit operator bool and an implicit operator int.