这里发生了什么?
#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,但只有左操作数增加了。
#include <iostream>
和using std::cout; using std::endl;
也许会需要。 - obatakucout
的代码问题中,#include <iostream>
和using namespace std;
只是噪音。如果这些都是缺少的,并且读者无法弄清如何使其编译,那么他们没有回答C++问题的业务能力。 - Benjamin Lindley