本文介绍了逗号运算符如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
逗号运算符在 C++ 中是如何工作的?
How does the comma operator work in C++?
例如,如果我这样做:
a = b, c;
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;
变成了
a = b, // <- Note comma typo!
c = d;
推荐答案
就等于 b
.
逗号运算符的优先级低于赋值.
The comma operator has a lower precedence than assignment.
这篇关于逗号运算符如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!