逗号操作员如何工作 [英] How does the Comma Operator work
本文介绍了逗号操作员如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
逗号运算子如何在C ++中运作?
例如,如果我这样做:
a = b,c;
最后是否等于b或c?
(是的,我知道这很容易测试 - 只需在这里记录即可快速找到答案。)
更新:此问题在使用逗号运算符时暴露了细微差别。只要记录下:
a = b,c; // a设置为b的值!
a =(b,c); // a设置为c的值!
这个问题实际上是受到代码错误的启发。
a = b;
c = d;
成为
a = b,// < - 注意逗号错误!
c = d;
解决方案
逗号运算符的优先级低于赋值。
How does the comma operator work in C++?
For instance, if I do:
a = b, c;
Does a end up equaling b or c?
(Yes, I know this is easy to test - just documenting on here for someone to find the answer quickly.)
Update: This question has exposed a nuance when using the comma operator. Just to document this:
a = b, c; // a is set to the value of b!
a = (b, c); // a is set to the value of c!
This question was actually inspired by a typo in code. What was intended to be
a = b;
c = d;
Turned into
a = b, // <- Note comma typo!
c = d;
解决方案
It would be equal to b.
The comma operator has a lower precedence than assignment.
这篇关于逗号操作员如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文