表达“变量,变量=值;”。 [英] Expression "variable, variable = value;"
问题描述
我一直在浏览一些MFC代码,并且遇到了这个表达式。它在OnInitDialog()函数中,看起来好像不是MFC特有的。变量有一个名字,值为0。
I have been looking through some MFC code and i came across this expression. It was in OnInitDialog() function, didn't look like it's MFC specific. The variables had some name, value was 0.
int volatile something, somethingElse; //this was global
something, somethingElse = 0; //this was inside the function
内部
在C ++中有意义吗?我知道逗号运算符是如何工作的,尽管它像这样的自由形式应该分隔表达式。变量名也是表达式吗?这段代码可以编译,因此它如何工作?
Does this make any sense in C++? I know how the comma operator works, though in a free form like here it should be separating expressions. Is a variable name also an expression? This code does compile, so how does this work?
推荐答案
这很可能是程序错误。语句
This is likely an error in the program. The statement
a, b = c;
完全等于
b = c;
由于逗号运算符从左到右求值,并丢弃除最后一个值以外的所有值。由于表达式a没有任何副作用,因此它基本上是无操作的。
Since the comma operator evaluates from left to right and discards all values except the last. Since the expression a has no side effects, it's essentially a no-op.
我会怀疑这是程序员错误还是来自其他语言的错误代码翻译进入C ++。您应该联系作者以使他们知道这一点。
I would suspect that this is either programmer error or an incorrect translation of code from a different language into C++. You should contact the author to let them know about this.
希望这会有所帮助!
这篇关于表达“变量,变量=值;”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!