C++ 我不知道为什么结果是这样的?我以为是15505
我不知道为什么结果是这样的?我以为是15505。 有人能帮我了解结果吗C++ 我不知道为什么结果是这样的?我以为是15505,c++,C++,我不知道为什么结果是这样的?我以为是15505。 有人能帮我了解结果吗 #include<iostream> #include<conio.h> using namespace std; int f(int &a, int &b) { int m = a; int n = b; a = a*b; b = a / b; return n + m; } int main() { int x = 10;
#include<iostream>
#include<conio.h>
using namespace std;
int f(int &a, int &b)
{
int m = a;
int n = b;
a = a*b;
b = a / b;
return n + m;
}
int main()
{
int x = 10;
int y = 5;
cout << f(x, y) << " " << x << " " << y;
_getch();
return 0;
}
//why is the result like this?
只需按如下方式调用函数:
int a = f(x, y);
cout << a << " " << x << " " << y;
结果是什么?我不知道你得到了什么,但预期是:15 50 10 vs 2015得到的结果是15 10 5。@Kasra跟随链接。问题是fx,y更新了x和y。我们不知道输出是显示旧值还是新值。这是未指定的。太棒了。它工作了。非常感谢。但原因是什么???我不能再发布答案了,所以没有完整的代码。很抱歉,但是如果你检查反汇编,首先Y被馈送到流中,然后,X被馈送,然后它调用f,并馈送结果。这是在VS2015上,因为评估顺序未指定。可能是从左到右或从右到左。我认为你是对的。你真是太好了