Operator Overloading Terminology Reference

This reference provides an overview of operator overloading terminology you'll encounter in this chapter. Use it as a quick lookup guide.

Operator Categories

Term Definition Example
Arithmetic Operator Operators for mathematical operations +, -, *, /, %
Comparison Operator Operators for comparing values ==, !=, <, >, <=, >=
Logical Operator Operators for boolean logic &&, ||, !
Bitwise Operator Operators for bit manipulation &, |, ^, ~, <<, >>
Assignment Operator Operators that assign values =, +=, -=, *=, /=
Increment/Decrement Operator Operators that modify by one ++, --
Subscript Operator Array/container element access []
Function Call Operator Makes object callable like function ()

Operator Implementation Forms

Term Definition Example
Member Function Operator Operator implemented as class member MyClass operator+(const MyClass& rhs) const
Non-member Function Operator Operator implemented as free function friend MyClass operator+(const MyClass& lhs, const MyClass& rhs)
Friend Function Non-member function with access to private members friend std::ostream& operator<<(std::ostream& os, const MyClass& obj)
Prefix Operator Increment/decrement applied before value is used ++x, returns reference
Postfix Operator Increment/decrement applied after value is used x++, returns copy, takes dummy int parameter

Special Operators

Term Definition Example
Stream Insertion Operator Outputs object to stream std::ostream& operator<<(std::ostream& os, const MyClass& obj)
Stream Extraction Operator Reads object from stream std::istream& operator>>(std::istream& is, MyClass& obj)
Subscript Operator Provides indexed access to elements T& operator[](size_t index)
Function Call Operator Makes object behave like function (functor) ReturnType operator()(ParamType param)
Arrow Operator Provides pointer-like member access T* operator->()
Dereference Operator Provides pointer-like dereferencing T& operator*()
Conversion Operator Implicit or explicit type conversion operator int() const
Spaceship Operator Three-way comparison (C++20) auto operator<=>(const MyClass&) const = default;

Return Type Patterns

Term Definition Example
Return by Value Returns copy of result, used for arithmetic MyClass operator+(const MyClass& rhs) const
Return by Reference Returns reference to object, used for assignment MyClass& operator=(const MyClass& rhs)
Return by Const Reference Returns const reference, prevents modification const T& operator[](size_t index) const
Return bool Returns boolean for comparisons bool operator==(const MyClass& rhs) const
Return this Returns reference to current object for chaining return *this; in assignment operators

Operator Overloading Principles

Term Definition Example
Symmetry Binary operators should treat operands equally Non-member for + to allow 5 + obj and obj + 5
Consistency Operators should behave as expected += should add and assign like built-in types
Chaining Operators return reference to enable chaining cout << a << b << c;
const Correctness const methods don't modify object bool operator==(const MyClass& rhs) const
noexcept Mark operators that won't throw exceptions MyClass& operator=(MyClass&& rhs) noexcept

Common Operator Idioms

Term Definition Example
Canonical Form Standard pattern for implementing operators Implement ==, then != using ==
Compound Assignment Implement +=, then + using += a + b returns a += b on temporary copy
Copy-and-Swap Safe assignment using swap MyClass& operator=(MyClass rhs) { swap(rhs); return *this; }
Relational Operators Derive all comparisons from < and == C++20 spaceship operator simplifies this
Smart Pointer Operators ->, *, and bool conversion for smart pointers Enable pointer-like interface

Advanced Concepts

Term Definition Example
Functor Object with overloaded operator() Used with STL algorithms
Proxy Object Temporary object returned by operator for delayed evaluation Used in expression templates
Expression Templates Advanced technique for optimizing compound expressions Used in linear algebra libraries
Overload Resolution Compiler selecting best matching operator Considers member vs non-member, conversions
User-defined Literal Custom suffix for literal values operator""_km(long double)

Operator Restrictions

Term Definition Example
Non-overloadable Operators Operators that cannot be overloaded ::, ., .*, ?:, sizeof, typeid
Arity Preservation Cannot change number of operands Binary + stays binary
Precedence Operator precedence cannot be changed * always higher precedence than +
Associativity Operator associativity cannot be changed Assignment remains right-associative
Default Meaning Cannot overload for built-in types Cannot change how int + int works

Stream Operators

Term Definition Example
Stream Insertion Output operator for writing to streams friend std::ostream& operator<<(std::ostream&, const T&)
Stream Extraction Input operator for reading from streams friend std::istream& operator>>(std::istream&, T&)
Stream Chaining Returning stream reference enables chaining cout << a << b;
Format Flags Stream state affecting formatting std::ios::fixed, std::setprecision
Stream State Checking stream validity after operations Check is.good() after reading