当我运行这个程序时,我得到奇怪的输出 1132。有人能解释一下为什么是 1132 吗?
//main.cpp
#include <iostream>
using namespace std;
int f1(int &a){return a++;}
int f2(int &a){return ++a;}
int main(){
int x, y;
int *px, *py;
x = 1;
y = 2;
px = &x;
py = &y;
cout << f1(*px) << *px << f2(*py) << *py << "\n";
return 0;
}
最佳答案
在您的案例中,评估顺序是从右到左。
请注意,不能保证从左到右求值。
所以顺序是:
*py
f2(*py)
*px
f1(*px)
然后才有<<
运算符运行(并按预期从左到右进行评估)
关于c++ - 奇怪的 cout 输出(代码块 codeblocks-16.01mingw-setup.exe),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39390646/