【发布时间】:2012-08-21 14:20:03
【问题描述】:
这里发生了什么?
#include <iostream>
using namespace std;
int main(){
int x=0,y=0;
true? ++x, ++y : --x, --y;
cout << "x: " << x << endl;
cout << "y: " << y << endl; //why does y=0 here?
x=0,y=0;
false ? ++x, ++y : --x, --y;
cout << "x: " << x << endl;
cout << "y: " << y << endl;
}
x: 1
y: 0
x: -1
y: -1
第二种情况似乎很好。在第一种情况下,我希望 x 和 y 都递增到 1,但只有左侧操作数递增。
【问题讨论】:
-
请提供实际编译的代码——否则我懒得帮你
-
@AdrianCornish 此代码编译;你试过了吗?
-
@chrisaycock 实际上没有。如果您添加
#include <iostream>和using std::cout; using std::endl;可能会这样做 -
是的。 OP 应该阅读 sscce.org,但在清晰度和细节方面仍然高于平均水平。
-
在使用
cout、#include <iostream>和using namespace std;的代码的问题中只是噪音。如果仅此而已,并且读者无法弄清楚如何编译它,那么他/她就没有资格回答 C++ 问题。
标签: c++ operator-precedence comma-operator