Quote:
Originally posted by Ikebo
++i %= 2; // does not work in C
++i %= 2; // works in C++
++i %= 2; // does not work in Java
C I can understand, but why is it that C++ allows a statement like that where even Java says no?
|
First realize that it's a bit of a weird thing to do because "++i" is an expression, and having an expression on the left side of an assignment ( =, %=, += or whatever) looks wrong when you think about it.
But C++ is Object Oriented, and allows you to
overload an operator for object classes, like the
++ operator for example. So, in C++, there can be (a reference to) an object on the left side of the assignment, which may have an
operator++ method defined. That's why C++ allows you to have a
++ on the left side of an assignment. For consistancy with this, the designers of C++ also implemented this for primary types, like integers, I suppose.
Java doesn't allow this because it does not allow operators to be overloaded.