【发布时间】:2016-11-11 01:28:55
【问题描述】:
int x = 1 , y = 1 , z = 1 ;
现在检查这些代码行:-
cout << (++x || ++y)<<endl; //Output 1
cout << x <<" " << y; // now x = 2 and y = 1 . Why 'y' is not incremented ?
再次将值初始化为 1
cout <<(++x && ++y )<<endl; //Output 1
cout << x <<" " << y; //now x = 2 and y = 2 . Why 'y' is incremented ?
再次将值初始化为 1
cout << (++x ||++y && ++z )<<endl; //Output 1
cout << x<<" "<< y<<" "<<z ; //now x = 2 , y = 1 , z = 1.Why these outputs?
有人可以解释一下编译器是如何读取这些代码的吗? 我阅读了有关优先顺序的信息,但我无法弄清楚编译器如何处理这些类型的代码。即使是小小的帮助也将不胜感激!
【问题讨论】:
-
查找“短路评估”。
-
一些额外的reading。