首先使用哪个运算符?

Which operator is used first?

我刚刚在我的 c++ 中进行了测试 class 我弄错的问题之一是:

Look at the following statement. while (x++ < 10) Which operator is used first?

我的答案是 ++ 但是,测试告诉我它实际上是 <。有人可以解释这是为什么吗?

你是对的。 operator++ 的优先级高于 operator<

C++ Operator Precedence

因此,在这种情况下,将首先调用 operator++,然后 return 将用于比较的原始值(递增之前)。

LIVE

我怀疑这是因为 x++ 是一个 post 增量。所以你可以说它首先将 x 与 10 进行比较,然后再将 1 与 x 相加。

如果是 ++x 那么添加会先完成。

我觉得这个问题有点坑,因为在运算符优先级上,++<.

优先级高

x 是具有用户定义的后缀 operator++ 的类型时,则必须首先评估 ++

对于 x 的内置类型,我不确定是否可以对排序进行说明。

这是优先顺序。

1  Parenthesis                    () []
2  Structure Access               .  ->
3  Unary                          ! ++ -- * &
4  Multiply,Divide,Modulus        * / %
5  Add,Subtract                   + -
6  Shift Right,Left               >> <<
7  Greater,Less than etc          > < => <=
8  Equal , Not Equal              ==  !=
9  Bitwise AND                    &
10 Bitwise OR                     |
11 Logical AND                    &&
12 Logical OR                     ||
13 Conditional Expression         ? :
14 Assignment                     = += -= etc
15 comma                          .