|
Re: Weird problem with if statements
In C/C++, it is common for mutators to have return values, so that you can cascade (is that the right word?) operations. For example,
a = b = c
works because the assignment operator is right-associative. It is read in as a = (b = c), then b = c is evaluated first, and its return value is plugged into a = <that>. If the expression b = c didn't return anything, you'd have a = <void> and the compiler would generate an error.
This is why a number of C library functions return a pointer to a buffer argument. For example, strcpy (that is, string copy), which takes in a destination memory address and a source address, returns the destination pointer when it's done copying. This allows you to do something with the return instead of just sticking it by itself in a statement and putting a semicolon after it.
Combining expressions in this way can make your code a bit more difficult to read, which is probably why Java doesn't support this style that well.
|