预&C、C++、Java 和 & 中的后增量运算符行为C# [英] Pre & post increment operator behavior in C, C++, Java, & C#
本文介绍了预&C、C++、Java 和 & 中的后增量运算符行为C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
免责声明:这不是真实世界的例子.这只是这些语言如何工作的理论问题.
C/C++、C# 和 Java 在 post & 方面到底有什么区别?预增量运算符?
What exactly are the differences between C/C++, C#, and Java when it comes to post & pre increment operators?
这是我在 VC++10、Java 1.6 和 C#4 中得到的
This is what I get with VC++10, Java 1.6, and C# 4
int a = 2;
int b = a++ + a++;
int c = ++a + a++ + a++;
+-----+------+------+----+
| C | C++ | Java | C# |
+-----+-----+------+------+----+
| a | 7 | 7 | 7 | 7 |
+-----+-----+------+------+----+
| b | 4 | 4 | 5 | 5 |
+-----+-----+------+------+----+
| c | 15 | 15 | 16 | 16 |
+-----+-----+------+------+----+
推荐答案
Java 和 C# 从左到右计算表达式,副作用立即可见.
Java and C# evaluate expressions from left to right, and the side-effects are visible immediately.
在 C++ 中,子表达式的求值顺序是未指定的,在没有中间序列点的情况下修改同一个对象两次是未定义的行为.
In C++, the order of evaluation of subexpressions is unspecified, and modifying the same object twice without an intervening sequence point is undefined behavior.
这篇关于预&C、C++、Java 和 & 中的后增量运算符行为C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文