有人这么写了一个操作符–>,在 C++ 中是什么意思,提问原链接
问题
有这么一行代码,有这么一个看起来像操作符(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)
。