表达式“变量,变量=值”

发布于 2025-01-01 11:21:06 字数 320 浏览 3 评论 0原文

我一直在查看一些 MFC 代码,发现了这个表达式。它在 OnInitDialog() 函数中,看起来不是 MFC 特定的。这些变量有一些名称,值为 0。

int volatile something, somethingElse; //this was global

something, somethingElse = 0; //this was inside the function

这在 C++ 中有意义吗?我知道逗号运算符是如何工作的,尽管在像这里这样的自由形式中它应该分隔表达式。变量名也是表达式吗?这段代码确实可以编译,那么它是如何工作的呢?

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

荒岛晴空 2025-01-08 11:21:06

这很可能是程序中的错误。该语句

a, b = c;

完全相当于

b = c;

Since 逗号运算符从左到右计算并丢弃除最后一个值之外的所有值。由于表达式 a 没有副作用,因此它本质上是一个空操作。

我怀疑这要么是程序员错误,要么是代码从不同语言到 C++ 的错误翻译。您应该联系作者让他们知道这一点。

希望这有帮助!

This is likely an error in the program. The statement

a, b = c;

Is completely equivalent to

b = c;

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.

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.

Hope this helps!

尹雨沫 2025-01-08 11:21:06

合法但值得怀疑。逗号之前的部分根本不执行任何操作。

Legal but questionable. The part before the comma doesn't do anything at all.

于我来说 2025-01-08 11:21:06
something, somethingElse = 0; 

这样做可能是为了避免变量 something 上出现未使用的变量警告,并将 somethingElse 变量初始化为 0

something, somethingElse = 0; 

probably, it is done to avoid the unused variable warning on variable something an to initialize the somethingElse variable to 0.

有深☉意 2025-01-08 11:21:06

这在 C++ 中有意义吗?

是的,从语法上来说确实如此,但是如果没有注释,您可能不知道开发人员的意图(如果有的话)除了可能抑制变量警告之外。

变量名也是表达式吗?

是的,变量本身就是一个表达式。前任。 if(<表达式>) if(某事)

这段代码可以编译,那么它是如何工作的呢?

它的工作原理是使用 逗号运算符 并忽略 something 的结果将 0 分配给 somethingElse。尽管某物被标记为易失性,但原始开发人员可能拥有一个仍然抱怨未使用变量的编译器,并且作为聪明的开发人员,他或她随后决定使用该语法来抑制。

Does this make any sense in C++?

Yes syntactically it does, but without comments you may not know the developers intentions were (if any) other than maybe suppressing a variable warning.

Is a variable name also an expression?

Yes a variable itself is an expression. Ex. if(<expression>) if(something)

This code does compile, so how does this work?

It works by using the comma operator and ignoring the result of something then assigning 0 to somethingElse. Although something was marked volatile the original developer may of had a compiler that still complained about unused variables and being the clever developer he or she was then decided to suppress with that syntax.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文