2015年3月26日 星期四

[Linux_C]004

來源為wiki的Operators in C and C++

方便未來 重載運算子
關於C++ operator介紹如下:

Arithmetic operators
Operator name
Syntax
Can overload
Included
in C
Prototype examples
As member of K
Basic assignmenta = bYesYesR& K::operator =(S b);
Additiona + bYesYesR K::operator +(S b);
Subtractiona - bYesYesR K::operator -(S b);
Unary plus (integer promotion)+aYesYesR K::operator +();
Unary minus (additive inverse)-aYesYesR K::operator -();
Multiplicationa * bYesYesR K::operator *(S b);
Divisiona / bYesYesR K::operator /(S b);
Modulo (integer remainder)a % bYesYesR K::operator %(S b);
Increment
Prefix++aYesYesR& 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--aYesYesR& 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 toa == bYesYesbool K::operator ==(S const& b);
Not equal toa != b
a not_eq b
YesYesbool K::operator !=(S const& b);
Greater thana > bYesYesbool K::operator >(S const& b);
Less thana < bYesYesbool K::operator <(S const& b);
Greater than or equal toa >= bYesYesbool K::operator >=(S const& b);
Less than or equal toa <= bYesYesbool 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
YesYesR K::operator !();
Logical ANDa && b
a and b
YesYesR K::operator &&(S b);
Logical ORa || b
a or b
YesYesR 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
YesYesR K::operator ~();
Bitwise ANDa & b
a bitand b
YesYesR K::operator &(S b);
Bitwise ORa | b
a bitor b
YesYesR K::operator |(S b);
Bitwise XORa ^ b
a xor b
YesYesR K::operator ^(S b);
Bitwise left shifta << bYesYesR K::operator <<(S b);
Bitwise right shifta >> bYesYesR K::operator >>(S b);
Member and pointer operators
Operator name
Syntax
Can overload
Included
in C
Prototype examples
As member of K
Array subscripta[b]YesYesR& K::operator [](S b);
Indirection ("object pointed to by a")*aYesYesR& K::operator *();
Address ("address of a")&aYesYesR K::operator &();
Structure dereference
("member b of object pointed to bya")
a->bYesYesR* K::operator ->();
Structure reference ("member b of object a")a.bNoYesN/A
Object pointed to by member
b of object pointed to by a
a->*bYesNoR& K::operator ->*(S b);
Object pointed to by member b of object aa.*bNoNoN/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)YesYesR K::operator ()(S a, T b, ...);
Commaa, bYesYesR K::operator ,(S b);
Ternary conditionala ? b : cNoYesN/A
Scope resolutiona::bNoNoN/A
User-defined literals
since C++11
"a"_bYesNoN/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-ofsizeof (a)
sizeof (type)
NoYesN/A
Size of parameter pack
since C++11
sizeof...(Args)NoNoN/A
Align-of
since C++11
alignof (type)
or _Alignof (type)
NoYesN/A
Allocate storagenew typeYesNovoid* K::operator new(size_t x);
Allocate storage (array)new type[n]YesNovoid* K::operator new[](size_t a);
Deallocate storage
(delete returns void so it isn't
strictly speaking an operator)
delete aYesNovoid K::operator delete(void *a);
Deallocate storage (array)delete[] aYesNovoid K::operator delete[](void *a);
Type identificationtypeid (a)
typeid (type)
NoNoN/A
static_cast conversionstatic_cast<type>(a)NoNoN/A
dynamic_cast conversiondynamic_cast<type>(a)NoNoN/A
const_cast conversionconst_cast<type>(a)NoNoN/A
reinterpret_cast conversionreinterpret_cast<type>(a)NoNoN/A
Exception check
since C++11
noexcept(a)NoNoN/A

沒有留言:

張貼留言

創用 CC 授權條款
我什麼都不會!!Eddie Sung製作,以創用CC 姓名標示-非商業性-相同方式分享 3.0 台灣 授權條款釋出。
此作品衍生自Eddie Sung