【发布时间】:2021-09-30 06:31:32
【问题描述】:
我正在学习 c++,目前正在学习运算符优先级。我正在玩以下示例。将每个部分想象成在不同时间运行的不同代码段,而不是同一方法中的多个代码块。
int b = 4;
int result = ++b;
// In the above example the result will be 5, as expected.
int b = 4;
int result = ++b + b;
// Here the result will be 10 as expected.
int b = 4;
int result = ++b + ++b;
这里的结果是 12。我不明白为什么。编译器不应该评估 ++b 将 4 更改为 5,然后将 ++b 更改为 6,从而导致 5+6 = 11?
【问题讨论】:
标签: c++ c++17 operators operator-precedence