【发布时间】:2013-07-25 17:17:49
【问题描述】:
int a=1,b=2,c=3;
int x=1;
int y=10;
a = x ? b : c;
cout<< a; // Outputs 2 (the value of b)
a = y ? b : c;
cout<< a; // Outputs 2 (the value of b)
现在,看看下面的内容。
a=0;
x=0;
a = x ? b : c;
cout<< a; // Outputs 3 (the value of c !!!!)
为什么会有这种不寻常的行为?仅当 a 和 x 都为 0 时,表达式的计算结果为 false,否则,始终为 true。请解释一下。
【问题讨论】:
-
嗯...实际上这种行为很常见。如果您的结论是 both
a和x必须是0以使a获得c的值,那么您没有正确阅读实验结果跨度> -
是的。我现在明白了。我总结错了。谢谢!
标签: c++ c ternary-operator conditional-operator