
我在 Google Groups 论坛 comp.lang.c++.moderated 上看到一个回答 Hidden Features and Dark Corners of C++/STL ,他贴的那段代码中,这个操作符(operator)-->没看懂是啥意思,我试了一下,在 Visual Studio 2008、G++ 4.4 和 gcc 上都可以编译通过,下面是代码:
#include <stdio.h>
int main()
{
int x = 10;
while (x --> 0) // x goes to 0
{
printf("%d ", x);
}
}谁能解释下这个操作符到底是什么意思?
-->不是一个操作符(operator),实际上是两个操作符(operator)合在一起了,即--和>。
在上面那段代码中,因为--是后自减,所以执行的顺序就是:先x > 0,然后x--。
说白了,上面的代码就等同于,
while( (x--) > 0 )其实如果你把代码拷贝到 Visual Studio 上,这个问题很快就可以解决,因为代码会自动格式化为while (x-- > 0)。
来自: https://stackoverflow.com/questions/1642028/what-is-the-operator-in-c