Is there any difference between the Java and C++ operators?

前端 未结 6 1001
后悔当初
后悔当初 2021-01-12 00:16

If you take Java\'s primitive numeric types, plus boolean, and compare it to C++ equivalent types, is there any difference what concerns the operators, like precedence rules

6条回答
  •  甜味超标
    2021-01-12 00:35

    Java specifies more about the order of evaluation of expressions than C++, and C++ says that you get undefined behavior if any of the legal evaluation orders of your expression modify an object twice between sequence points.

    So, i++ + i++ is well defined in Java, but has undefined behavior (no diagnosis required) in C++. Therefore you can't blindly copy expressions from Java to C++.

    For bitwise operators, Java specifies two's-complement representation of negative numbers whereas C++ doesn't. Not that you're likely to encounter another representation in C++, but if you did then you would find for example that -2 | 1 is always -1 in Java, but is -2 in a C++ implementation using 1s' complement.

提交回复
热议问题