c++符号优先级

来源:互联网 发布:阿里云拍照核验点 编辑:程序博客网 时间:2024/06/10 05:20

Operator Description Example Overloadable Group 1 (no associativity) ::Scope resolution operatorClass::age = 2;NOGroup 2 ()Function callisdigit('1')YES()Member initalization c_tor(int x, int y) : _x(x), _y(y*10){};YES[]Array accessarray[4] = 2;YES->Member access from a pointerptr->age = 34;YES.Member access from an objectobj.age = 34;NO++Post-incrementfor( int i = 0; i < 10; i++ ) cout << i;YES--Post-decrementfor( int i = 10; i > 0; i-- ) cout << i;YESconst_castSpecial castconst_cast<type_to>(type_from);NOdynamic_castSpecial castdynamic_cast<type_to>(type_from);NOstatic_castSpecial caststatic_cast<type_to>(type_from);NOreinterpret_castSpecial castreinterpret_cast<type_to>(type_from);NOtypeidRuntime type informationcout « typeid(var).name();
cout « typeid(type).name();NOGroup 3 (right-to-left associativity) !Logical negationif( !done ) …YESnotAlternate spelling for ! ~Bitwise complementflags = ~flags;YEScomplAlternate spelling for ~ ++Pre-incrementfor( i = 0; i < 10; ++i ) cout << i;YES--Pre-decrementfor( i = 10; i > 0; --i ) cout << i;YES-Unary minusint i = -1;YES+Unary plusint i = +1;YES*Dereferenceint data = *intPtr;YES&Address ofint *intPtr = &data;YESnewDynamic memory allocationlong *pVar = new long;
MyClass *ptr = new MyClass(args);YESnew []Dynamic memory allocation of arraylong *array = new long[n];YESdeleteDeallocating the memorydelete pVar;YESdelete []Deallocating the memory of arraydelete [] array;YES(type)Cast to a given typeint i = (int) floatNum;YESsizeofReturn size of an object or typeint size = sizeof floatNum;
int size = sizeof(float);NOGroup 4 ->*Member pointer selectorptr->*var = 24;YES.*Member object selectorobj.*var = 24;NOGroup 5 *Multiplicationint i = 2 * 4;YES/Divisionfloat f = 10.0 / 3.0;YES%Modulusint rem = 4 % 3;YESGroup 6 +Additionint i = 2 + 3;YES-Subtractionint i = 5 - 1;YESGroup 7 <<Bitwise shift leftint flags = 33 << 1;YES>>Bitwise shift rightint flags = 33 >> 1;YESGroup 8 <Comparison less-thanif( i < 42 ) …YES<=Comparison less-than-or-equal-toif( i <= 42 ) ...YES>Comparison greater-thanif( i > 42 ) …YES>=Comparison greater-than-or-equal-toif( i >= 42 ) ...YESGroup 9 ==Comparison equal-toif( i == 42 ) ...YESeqAlternate spelling for == !=Comparison not-equal-toif( i != 42 ) …YESnot_eqAlternate spelling for != Group 10 &Bitwise ANDflags = flags & 42;YESbitandAlternate spelling for & Group 11 ^Bitwise exclusive OR (XOR)flags = flags ^ 42;YESxorAlternate spelling for ^ Group 12 |Bitwise inclusive (normal) ORflags = flags | 42;YESbitorAlternate spelling for | Group 13 &&Logical ANDif( conditionA && conditionB ) …YESandAlternate spelling for && Group 14 ||Logical ORif( conditionA || conditionB ) ...YESorAlternate spelling for || Group 15 (right-to-left associativity) ? :Ternary conditional (if-then-else)int i = (a > b) ? a : b;NOGroup 16 (right-to-left associativity) =Assignment operatorint a = b;YES+=Increment and assigna += 3;YES-=Decrement and assignb -= 4;YES*=Multiply and assigna *= 5;YES/=Divide and assigna /= 2;YES%=Modulo and assigna %= 3;YES&=Bitwise AND and assignflags &= new_flags;YESand_eqAlternate spelling for &= ^=Bitwise exclusive or (XOR) and assignflags ^= new_flags;YESxor_eqAlternate spelling for ^= |=Bitwise normal OR and assignflags |= new_flags;YESor_eqAlternate spelling for |= <<=Bitwise shift left and assignflags <<= 2;YES>>=Bitwise shift right and assignflags >>= 2;YESGroup 17 throwthrow exceptionthrow EClass(“Message”);NOGroup 18 ,Sequential evaluation operatorfor( i = 0, j = 0; i < 10; i++, j++ ) …YES

原创粉丝点击