递增和按位移位运算符优先级?
Increment and bitwise shift operator precedence?
增量后缀和前缀的运算符优先级都高于按位左移,但下面打印出不同的结果。
int testValue = 1;
std::cout << ++testValue; //prints 2
//saw it as std::cout << (++testValue);
testValue = 1;
std::cout << testValue++; //prints 1
//thought it was std::cout << (testValue++);
为什么第二个示例中显示的递增后缀在插入之前打印 1?我认为它像增量前缀案例一样更紧密地绑定到testValue
,所以我认为它会先递增,然后再用std::cout
打印。
<<
不是按位运算符,而是insertion operator
。 cplusplus参考:
作为类 ostream 的对象,字符可以写入其中 作为使用插入运算符(运算符<<)的格式化数据。
testValue = 1;
std::cout << testValue++; //prints 1
后置增量表示在读取变量后完成增量。
你误解了testValue++
的作用。
它递增testValue
但其评估是在递增之前进行的。
该行
std::cout << testValue++;
相当于
std::cout << testValue;
testValue = testValue + 1;
相关文章:
- C++ 带有函数指针的运算符优先级
- <<运算符优先级与字符串比较
- 在一个指令中声明更多指针的运算符优先级
- C++中的运算符优先级
- c++中的增量和三元运算符优先级
- 涉及模运算符优先级的错误以及C++中具有大数字的括号
- 带有 cout 和指针的运算符优先级
- C++ 使用矢量迭代器时的运算符优先级
- 序列点和运算符优先级有什么区别
- 运算符优先级 括号问题
- C++表达式中的运算符优先级
- 相等运算符优先级不起作用
- 递增和按位移位运算符优先级?
- 运算符优先级超载在C 中
- C++ 中的递增/递减和输出流运算符优先级
- 输出流中的运算符优先级C++
- 运算符优先级C++
- C++11 何时相对于运算符优先级进行算术类型转换
- 从表中了解运算符优先级
- 是否违反 C/C++ 运算符优先级和关联性规则?