zoukankan      html  css  js  c++  java
  • C++ 运算符优先级

    C++ Operator Precedence

    The operators at the top of this list are evaluated first. Operators within a group have the same precedence. All operators have left-to-right associativity unless otherwise noted.

    Operator Description Example
    Group 1 (no associativity)
    :: Scope resolution operator Class::age = 2;
    Group 2
    () Grouping operator (a + b) / 4;
    [] Array access array[4] = 2;
    -> Member access from a pointer ptr->age = 34;
    . Member access from an object obj.age = 34;
    ++ Post-increment for( int i = 0; i < 10; i++ ) cout << i;
    -- Post-decrement for( int i = 10; i > 0; i-- ) cout << i;
    Group 3 (right-to-left associativity)
    ! Logical negation if( !done ) …
    ~ Bitwise complement flags = ~flags;
    ++ Pre-increment for( i = 0; i < 10; ++i ) cout << i;
    -- Pre-decrement for( i = 10; i > 0; --i ) cout << i;
    - Unary minus int i = -1;
    + Unary plus int i = +1;
    * Dereference int data = *intPtr;
    & Address of int *intPtr = &data;
    (type) Cast to a given type int i = (int) floatNum;
    sizeof Return size of an object int size = sizeof(floatNum);
    Group 4
    ->* Member pointer selector ptr->*var = 24;
    .* Member object selector obj.*var = 24;
    Group 5
    * Multiplication int i = 2 * 4;
    / Division float f = 10.0 / 3.0;
    % Modulus int rem = 4 % 3;
    Group 6
    + Addition int i = 2 + 3;
    - Subtraction int i = 5 - 1;
    Group 7
    << Bitwise shift left int flags = 33 << 1;
    >> Bitwise shift right int flags = 33 >> 1;
    Group 8
    < Comparison less-than if( i < 42 ) …
    <= Comparison less-than-or-equal-to if( i <= 42 ) ...
    > Comparison greater-than if( i > 42 ) …
    >= Comparison geater-than-or-equal-to if( i >= 42 ) ...
    Group 9
    == Comparison equal-to if( i == 42 ) ...
    != Comparison not-equal-to if( i != 42 ) …
    Group 10
    & Bitwise AND flags = flags & 42;
    Group 11
    ^ Bitwise exclusive OR (XOR) flags = flags ^ 42;
    Group 12
    | Bitwise inclusive (normal) OR flags = flags | 42;
    Group 13
    && Logical AND if( conditionA && conditionB ) …
    Group 14
    || Logical OR if( conditionA || conditionB ) ...
    Group 15
    ? : Ternary conditional (if-then-else) int i = (a > b) ? a : b;
    Group 16 (right-to-left associativity)
    = Assignment operator int a = b;
    += Increment and assign a += 3;
    -= Decrement and assign b -= 4;
    *= Multiply and assign a *= 5;
    /= Divide and assign a /= 2;
    %= Modulo and assign a %= 3;
    &= Bitwise AND and assign flags &= new_flags;
    ^= Bitwise exclusive or (XOR) and assign flags ^= new_flags;
    |= Bitwise normal OR and assign flags |= new_flags;
    <<= Bitwise shift left and assign flags <<= 2;
    >>= Bitwise shift right and assign flags >>= 2;
    Group 17
    , Sequential evaluation operator for( i = 0, j = 0; i < 10; i++, j++ ) …

    Order of Evaluation and of Side Effects

    One important aspect of C++ that is related to operator precedence is the order of evaluation and the order of side effects in expressions. In some circumstances, the order in which things happen is not defined. For example, consider the following code:

        float x = 1;
        x = x / ++x;

    The value of x is not guaranteed to be consistent across different compilers, because it is not clear whether the computer should evaluate the left or the right side of the division first. Depending on which side is evaluated first, x could take a different value.

    Furthermore, while ++x evaluates to x+1, the side effect of actually storing that new value in x could happen at different times, resulting in different values for x.

    The bottom line is that expressions like the one above are horribly ambiguous and should be avoided at all costs. When in doubt, break a single ambiguous expression into multiple expressions to ensure that the order of evaluation is correct.

  • 相关阅读:
    小程序swiper组件实现间距轮播
    小程序form静态页面跳转
    批量添加Iconfont图标库图标
    Vant Weapp 有赞小程序UI库 ICON 组件的本地图标路径支持
    $rootScope、$apply、$watch
    EF code first 数据模型创建数据库
    angularjs directive2
    angularjs directive
    angularjs service
    angular repeat
  • 原文地址:https://www.cnblogs.com/secbook/p/2655454.html
Copyright © 2011-2022 走看看