以下表格为操作符重载推荐的使用方法

OPERATOR NAME OR CATEGOTY METHORD OR GLOBAL FUNCTION WHEN TO OVERLOAD SAMPLE PROTOTYPES
  • operator+
  • operator-
  • operator*
  • operator/
  • operator%
Binary arithmetic Global function recommended Whenever you want to provide these operations for your class
  • T operator+(const T&, const T&);
  • T operator+(const T&, const E&);
  • operator-
  • operator+
  • operator~
Unary arithmetic and bitwise operators Method recommended Whenever you want to provide these operations for your class
  • T operator-() const;
  • operator++
  • operator--
Pre-increment and pre-decrement Method recommended Whenever you overload += and -= taking an arithmetic argument (int, long, . . .)
  • T& operator++();
  • operator++
  • operator--
Post-increment and post-decrement Method recommended Whenever you overload += and -= taking an arithmetic argument (int, long, . . .)
  • T operator++(int);
  • operator=
Assignment operator Method recommended Whenever your class has dynamically allocated resources, or members that are references
  • T& operator= (const T&);
  • operator+=
  • operator-=
  • operator*=
  • operator/=
  • operator%=
Shorthand arithmetic operator assignments Method recommended Whenever you overload the binary arithmetic operators and your class is not designed to be immutable
  • T& operator+= (const T&);
  • T& operator+= (const E&);
  • operator<<
  • operator>>
  • operator&
  • operator|
  • operator^
Binary bitwise operators Global function recommended Whenever you want to provide these operations
  • T operator<< (const T&, const T&);
  • T operator<< (const T&, const E&);
  • operator<<=
  • operator>>=
  • operator&=
  • operator|=
  • operator^=
Shorthand bitwise operator assignments Method recommended Whenever you overload the binary bitwise operators and your class is not designed to be immutable
  • T& operator<<= (const T&);
  • T& operator<<= (const E&);
  • operator<=>
Three-way comparison operator Method recommended Whenever you want to provide comparison support for your class
  • auto operator<=> (const T&) const;
  • partial_ordering operator<=>(const E&) const;
  • operator==
Binary equality operator
  • C++20: method recommended
  • Pre-C++20: global function recommended
Whenever you want to provide comparison support for your class
  • bool operator== (const T&) const;
  • bool operator== (const E&) const;
  • bool operator== (const T&, const T&);
  • bool operator== (const T&, const E&);
  • operator!=
Binary inequality operator
  • C++20: method recommended
  • Pre-C++20: global function recommended
  • C++20: not needed as the compiler automatically provides != when you provide ==
  • Pre-C++20: Whenever you want to provide comparison support for your class
  • bool operator!= (const T&) const;
  • bool operator!= (const E&) const;
  • bool operator!= (const T&, const T&);
  • bool operator!= (const T&, const E&);
  • operator<
  • operator>
  • operator<=
  • operator>=
Binary comparison operators Global function recommended Whenever you want to provide these operations
  • bool operator< (const T&, const T&);
  • bool operator< (const T&, const E&);
  • operator<<
  • operator>>
I/O stream operators (insertion and extraction) Global function required Whenever you want to provide these operations
  • ostream& operator<< (ostream&, const T&);
  • istream& operator>> (istream&, T&);
  • operator!
Boolean negation operator Method recommended Rarely; use bool or void* conversion instead
  • bool operator!() const;
  • operator&&
  • operator||
Binary Boolean operators Global function recommended Rarely, if ever, because you lose short-circuiting; it’s better to overload & and instead, as these never short-circuit
  • operator[]
Subscripting (array index) operator Method required When you want to support subscripting
  • E& operator[](size_t);
  • const E& operator[] (size_t) const;
  • operator()
Function call operator Method required When you want objects to behave like function pointers, or for multidimensional array access, since [] can have only one index Return type and parameters can vary; see later examples in this chapter
  • operator type()
Conversion, or cast, operators (separate operator for each type) Method required When you want to provide conversions from your class to other types
  • operator double() const;
  • operator ""_x
User-defined literal operator Global function required When you want to support user-defined literals
  • T operator""_i(long double d);
  • operator new
  • operator new[]
Memory allocation routines Method recommended When you want to control memory allocation for your classes (rarely)
  • void* operator new(size_t size);
  • void* operator new[](size_t size);
  • operator delete
  • operator delete[]
Memory deallocation routines Method recommended Whenever you overload the memory allocation routines (rarely)
  • void operator delete(void* ptr) noexcept;
  • void operator delete[](void* ptr) noexcept;
  • operator*
  • operator->
Dereferencing operators Method recommended for operator* Method required for operator-> Useful for smart pointers
  • E& operator*() const;
  • E* operator->() const;

操作符优先级

  • 以下表格中 T 代表类型,x, y, z 代表对象
PRECEDENCE OPERATOR ASSOCIATIVITY
1 :: Left-to-right
2 x++, x--, x(), x[], T(), T{}, ., -> Left-to-right
3 ++x, --x, +x, -x, !, ~, *x, &x, (T), sizeof, co_await, new, delete, new[], delete[] Right-to-left
4 .*, ->* Left-to-right
5 x*y, x/y, x%y Left-to-right
6 x+y, x-y Left-to-right
7 <<, >> Left-to-right
8 <=> Left-to-right
9 <, <=, >, >= Left-to-right
10 ==, != Left-to-right
11 & Left-to-right
12 ^ Left-to-right
13 | Left-to-right
14 && Left-to-right
15 || Left-to-right
16 x?y:z, throw, co_yield, =, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |= Right-to-left
17 , Left-to-right