方便未來 重載運算子 用
關於C++ operator介紹如下:
| Arithmetic operators | |||||
Operator name
|
Syntax
|
Can overload
|
Included
in C | Prototype examples | |
| As member of K | |||||
| Basic assignment | a = b | Yes | Yes | R& K::operator =(S b); | |
| Addition | a + b | Yes | Yes | R K::operator +(S b); | |
| Subtraction | a - b | Yes | Yes | R K::operator -(S b); | |
| Unary plus (integer promotion) | +a | Yes | Yes | R K::operator +(); | |
| Unary minus (additive inverse) | -a | Yes | Yes | R K::operator -(); | |
| Multiplication | a * b | Yes | Yes | R K::operator *(S b); | |
| Division | a / b | Yes | Yes | R K::operator /(S b); | |
| Modulo (integer remainder) | a % b | Yes | Yes | R K::operator %(S b); | |
Increment
| Prefix | ++a | Yes | Yes | R& K::operator ++(); |
Postfix
|
a++
|
Yes
|
Yes
| R K::operator ++(int); | |
| Note: C++ uses the unnamed dummy-parameter int to differentiate between prefix and suffix increment operators. | |||||
Decrement
| Prefix | --a | Yes | Yes | R& K::operator --(); |
Postfix
|
a--
|
Yes
|
Yes
| R K::operator --(int); | |
| "Note: C++ uses the unnamed dummy-parameter int to differentiate between prefix and suffix decrement operators." | |||||
| Comparison operators/relational operators | |||||
Operator name
|
Syntax
|
Can overload
|
Included
in C | Prototype examples | |
| As member of K | |||||
| Equal to | a == b | Yes | Yes | bool K::operator ==(S const& b); | |
| Not equal to | a != b a not_eq b | Yes | Yes | bool K::operator !=(S const& b); | |
| Greater than | a > b | Yes | Yes | bool K::operator >(S const& b); | |
| Less than | a < b | Yes | Yes | bool K::operator <(S const& b); | |
| Greater than or equal to | a >= b | Yes | Yes | bool K::operator >=(S const& b); | |
| Less than or equal to | a <= b | Yes | Yes | bool K::operator <=(S const& b); | |
| Logical operators | |||||
Operator name
|
Syntax
|
Can overload
|
Included
in C | Prototype examples | |
| As member of K | |||||
| Logical negation (NOT) | !a not a | Yes | Yes | R K::operator !(); | |
| Logical AND | a && b a and b | Yes | Yes | R K::operator &&(S b); | |
| Logical OR | a || b a or b | Yes | Yes | R K::operator ||(S b); | |
| Bitwise operators | |||||
Operator name
|
Syntax
|
Can overload
|
Included
in C | Prototype examples | |
| As member of K | |||||
| Bitwise NOT | ~a compl a | Yes | Yes | R K::operator ~(); | |
| Bitwise AND | a & b a bitand b | Yes | Yes | R K::operator &(S b); | |
| Bitwise OR | a | b a bitor b | Yes | Yes | R K::operator |(S b); | |
| Bitwise XOR | a ^ b a xor b | Yes | Yes | R K::operator ^(S b); | |
| Bitwise left shift | a << b | Yes | Yes | R K::operator <<(S b); | |
| Bitwise right shift | a >> b | Yes | Yes | R K::operator >>(S b); | |
| Member and pointer operators | |||||
Operator name
|
Syntax
|
Can overload
|
Included
in C | Prototype examples | |
| As member of K | |||||
| Array subscript | a[b] | Yes | Yes | R& K::operator [](S b); | |
| Indirection ("object pointed to by a") | *a | Yes | Yes | R& K::operator *(); | |
| Address ("address of a") | &a | Yes | Yes | R K::operator &(); | |
| Structure dereference ("member b of object pointed to bya") | a->b | Yes | Yes | R* K::operator ->(); | |
| Structure reference ("member b of object a") | a.b | No | Yes | N/A | |
| Object pointed to by member b of object pointed to by a | a->*b | Yes | No | R& K::operator ->*(S b); | |
| Object pointed to by member b of object a | a.*b | No | No | N/A | |
| Other operators | |||||
Operator name
|
Syntax
|
Can overload
|
Included
in C | Prototype examples | |
| As member of K | |||||
| Function call See Function object. | a(a1, a2) | Yes | Yes | R K::operator ()(S a, T b, ...); | |
| Comma | a, b | Yes | Yes | R K::operator ,(S b); | |
| Ternary conditional | a ? b : c | No | Yes | N/A | |
| Scope resolution | a::b | No | No | N/A | |
| User-defined literals since C++11 | "a"_b | Yes | No | N/A | |
Conversion ( C-style cast)
|
(type) a
type(a) |
Yes
|
Yes
| K::operator R(); | |
| Note: for user-defined conversions, the return type implicitly and necessarily matches the operator name. | |||||
| Size-of | sizeof (a) sizeof (type) | No | Yes | N/A | |
| Size of parameter pack since C++11 | sizeof...(Args) | No | No | N/A | |
| Align-of since C++11 | alignof (type) or _Alignof (type) | No | Yes | N/A | |
| Allocate storage | new type | Yes | No | void* K::operator new(size_t x); | |
| Allocate storage (array) | new type[n] | Yes | No | void* K::operator new[](size_t a); | |
| Deallocate storage (delete returns void so it isn't strictly speaking an operator) | delete a | Yes | No | void K::operator delete(void *a); | |
| Deallocate storage (array) | delete[] a | Yes | No | void K::operator delete[](void *a); | |
| Type identification | typeid (a) typeid (type) | No | No | N/A | |
| static_cast conversion | static_cast<type>(a) | No | No | N/A | |
| dynamic_cast conversion | dynamic_cast<type>(a) | No | No | N/A | |
| const_cast conversion | const_cast<type>(a) | No | No | N/A | |
| reinterpret_cast conversion | reinterpret_cast<type>(a) | No | No | N/A | |
| Exception check since C++11 | noexcept(a) | No | No | N/A | |

沒有留言:
張貼留言