帮助?为什么输出是这样的?

发布于 2024-11-07 13:09:19 字数 357 浏览 0 评论 0原文

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

滴情不沾 2024-11-14 13:09:19

C++ 中未指定函数求值的顺序。在代码中:

cout << g() << " " << f() << " " << g() + f() << endl;

编译器可以发出代码来调用 f()、f()、g()、g(),然后将结果相加。或者它可以做其他事情。

这与 cout 没有什么特定的关系,顺便说一句 - 如果你编写这样的代码:

x = a() + b() * c();

无法保证 a、b 和 c 将被调用的顺序。这是全局变量是一件坏事的众多原因之一 -您通常无法预测如何调用更改它们的函数。

The order of evaluation of functions is unspecified in C++. In the code:

cout << g() << " " << f() << " " << g() + f() << endl;

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:

x = a() + b() * c();

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.

〆一缕阳光ご 2024-11-14 13:09:19

它与评估顺序有关。在这种情况下,首先评估 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

北笙凉宸 2024-11-14 13:09:19

对于此特定结果,首先计算 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 in a eventually being incremented to 10 and the result being 18. This is the case regardless of whether the g() or f() bit of that sum is done first. Doing g() first gives 8+10, otherwise it's 9+9.

Then f() is evaluated, setting a to 11 and returning 11.

Then g() is evaluated, setting a to 12 and returning 11.

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 :-)

金兰素衣 2024-11-14 13:09:19

除非表达式中存在序列点,否则无法保证执行顺序。如果您想知道此处的顺序,则必须将其分解为单独的语句。

cout << g() << " ";
cout << f() << " ";
int temp = g();
temp += f();
cout << temp << endl;

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.

cout << g() << " ";
cout << f() << " ";
int temp = g();
temp += f();
cout << temp << endl;
雪化雨蝶 2024-11-14 13:09:19

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

会发光的星星闪亮亮i 2024-11-14 13:09:19
cout << g() << " " << f() << " " << g() + f() << endl;

相同是发生这种情况的原因。

cout.operator<<(operator<<(operator<<(operator<<(operator<<(operator<<(endl), g() + f()), " "), f()), " "), g());

与调用函数的顺序

cout << g() << " " << f() << " " << g() + f() << endl;

same as

cout.operator<<(operator<<(operator<<(operator<<(operator<<(operator<<(endl), g() + f()), " "), f()), " "), g());

The order the functions are called is why this occurs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文