帮助?为什么输出是这样的?
#include <iostream>
using namespace std;
int a = 8;
int g()
{
a++;
return a - 1;
}
int f()
{
a++;
return a;
}
int main()
{
cout << g() << " " << f() << " " << g() + f() << endl;
system("PAUSE");
return 0;
}
输出为“11 11 18”
#include <iostream>
using namespace std;
int a = 8;
int g()
{
a++;
return a - 1;
}
int f()
{
a++;
return a;
}
int main()
{
cout << g() << " " << f() << " " << g() + f() << endl;
system("PAUSE");
return 0;
}
Output is "11 11 18"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
C++ 中未指定函数求值的顺序。在代码中:
编译器可以发出代码来调用 f()、f()、g()、g(),然后将结果相加。或者它可以做其他事情。
这与 cout 没有什么特定的关系,顺便说一句 - 如果你编写这样的代码:
无法保证 a、b 和 c 将被调用的顺序。这是全局变量是一件坏事的众多原因之一 -您通常无法预测如何调用更改它们的函数。
The order of evaluation of functions is unspecified in C++. In the code:
The compiler could emit code to call f(), f(), g(), g() and then add the results. Or it could do something else.
This is nothing specific to use with cout, BTW - if you write code like this:
There is no guarantee about which order a, b and c will be called in. This is one of the many reasons that global variables are A Bad Thing - you often cannot predict how functions that change them will be called.
它与评估顺序有关。在这种情况下,首先评估 g() + f(),从而给出 8 + 10 = 18。之后 a == 10 并评估 f(),得到 11 并将 a 设置为 11 等
It has to do with order of evaluation. In this case g() + f() is evaluated first, thus giving 8 + 10 = 18. After this a == 10 and f() is evaluated which gives 11 and sets a to 11 etc
对于此特定结果,首先计算
g() + f()
,这将导致a
最终递增到10
并且结果18
。无论该和的g()
还是f()
位先完成,情况都是如此。首先执行g()
给出8+10
,否则给出9+9
。然后计算
f()
,将a
设置为11
并返回11
。然后计算
g()
,将a
设置为12
并返回11
。换句话说,它首先调用
cout
的最右边的位,然后向左处理。现在你会注意到我上面的漫谈中的短语“为了这个特定的结果”。这是否是标准强制要求的,我不确定(这意味着我现在懒得去查找它),但根据经验,我非常怀疑它。
因此,虽然它实际上以正确的顺序输出项目,但副作用可能会根据很多因素而有所不同。这就是为什么全局变量(或者更准确地说,对它们产生副作用)很少是一个好主意的原因之一,您可能应该重新考虑对它们的使用:-)
For this particular result,
g() + f()
is being evaluated first which will result ina
eventually being incremented to10
and the result being18
. This is the case regardless of whether theg()
orf()
bit of that sum is done first. Doingg()
first gives8+10
, otherwise it's9+9
.Then
f()
is evaluated, settinga
to11
and returning11
.Then
g()
is evaluated, settinga
to12
and returning11
.In other words, it's calling the right-most bits of the
cout
first and proceeding left.Now you'll notice the phrase "for this particular result" in my ramblings above. Whether this is mandated by the standard, I don't know for sure (meaning I couldn't be bothered looking it up at the moment), but I very much doubt it, based on experience.
So, while it's actually outputting the items in the correct order, the side effects can be different depending on a large number of things. That's one reason why global variables (or, more correctly, side effects on them) are rarely a good idea and you should probably rethink your use of them :-)
除非表达式中存在序列点,否则无法保证执行顺序。如果您想知道此处的顺序,则必须将其分解为单独的语句。
The order of execution is not guaranteed unless there is a sequence point in the expression. If you want to know the order here you have to break it into separate statements.
a 是一个全局变量,因此可以被所有人访问。
此外,运算符<<从右向左访问,所以:
g() + f() = 8+10=18(10后的a)
f() = 11 且 a 为 11
g() = 11 且 a 为 12
a is a global variable and thus is being accessed by all.
in addition, operator<< is being accessed from right to left, so:
g() + f() = 8+10=18 (a after it is 10)
f() = 11 and a is 11
g() = 11 and a is 12
相同是发生这种情况的原因。
与调用函数的顺序
same as
The order the functions are called is why this occurs.