操作符重载
以下表格为操作符重载推荐的使用方法
| OPERATOR | NAME OR CATEGOTY | METHORD OR GLOBAL FUNCTION | WHEN TO OVERLOAD | SAMPLE PROTOTYPES | |||
|---|---|---|---|---|---|---|---|
|
Binary arithmetic | Global function recommended | Whenever you want to provide these operations for your class |
|
|||
|
Unary arithmetic and bitwise operators | Method recommended | Whenever you want to provide these operations for your class |
|
|||
|
Pre-increment and pre-decrement | Method recommended | Whenever you overload += and -= taking an arithmetic argument (int, long, . . .) |
|
|||
|
Post-increment and post-decrement | Method recommended | Whenever you overload += and -= taking an arithmetic argument (int, long, . . .) |
|
|||
|
Assignment operator | Method recommended | Whenever your class has dynamically allocated resources, or members that are references |
|
|||
|
Shorthand arithmetic operator assignments | Method recommended | Whenever you overload the binary arithmetic operators and your class is not designed to be immutable |
|
|||
|
</li><li>operator^`</li></ul> |
Binary bitwise operators | Global function recommended | Whenever you want to provide these operations |
|
||
|
=</li><li>operator^=`</li></ul> |
Shorthand bitwise operator assignments | Method recommended | Whenever you overload the binary bitwise operators and your class is not designed to be immutable |
|
||
|
Three-way comparison operator | Method recommended | Whenever you want to provide comparison support for your class |
|
|||
|
Binary equality operator |
|
Whenever you want to provide comparison support for your class |
|
|||
|
Binary inequality operator |
|
|
|
|||
|
Binary comparison operators | Global function recommended | Whenever you want to provide these operations |
|
|||
|
I/O stream operators (insertion and extraction) | Global function required | Whenever you want to provide these operations |
|
|||
|
Boolean negation operator | Method recommended | Rarely; use bool or void* conversion instead |
|
|||
|
`</li></ul> | 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 |
|
|
|
Subscripting (array index) operator | Method required | When you want to support subscripting |
|
|||
|
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 | |||
|
Conversion, or cast, operators (separate operator for each type) | Method required | When you want to provide conversions from your class to other types |
|
|||
|
User-defined literal operator | Global function required | When you want to support user-defined literals |
|
|||
|
Memory allocation routines | Method recommended | When you want to control memory allocation for your classes (rarely) |
|
|||
|
Memory deallocation routines | Method recommended | Whenever you overload the memory allocation routines (rarely) |
|
|||
|
Dereferencing operators | Method recommended for operator* Method required for operator-> |
Useful for smart pointers |
|
操作符优先级
- 以下表格中
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 |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Busyboxs!
