cout的<<关于操作员优先级的操作员工作? [英] How does cout's << operator work with regard to operator precedence?

查看:107
本文介绍了cout的<<关于操作员优先级的操作员工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
意外的评估顺序(编译器错误?)

Possible Duplicate:
Unexpected order of evaluation (compiler bug?)

我无法预测该程序的输出:

I couldn't predict the output for this program :

#include<iostream>
using namespace std;

int *p(int *a)
{
    (*a)++;
    return a;
}
int main()
{
    int i=0;

    cout<<i++<<" "<<(*p(&i))++<<" "<<i++<<" "<<i<<endl;
    return 0;
}

在vs2008中编译时,它输出3 2 0 4.有人可以解释为什么不是0 2 3 4吗?

When compiled in vs2008, it outputs 3 2 0 4. Can anybody explain why it's not 0 2 3 4 ?

注意:如果没有函数调用p,则效果很好.

Note: It works great if there is no function call to p.

提前谢谢!

推荐答案

未定义的行为.什么都可以做.

Undefined behaviour. Could do anything.

有关详细说明,请参见此答案.

See this answer for a good explanation.

这篇关于cout的&lt;&lt;关于操作员优先级的操作员工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆