hey guys,
So, in the code below there's this piece of code that could be written in a way simpler way and still give the same result, but I've seen this probably unnecessary ( maybe not ) complexity applied in a couple of projects.
C++ has both pre-increment and post increment operators.
With pre-increment (++p), the value is incremented before the value is used.
With post-increment (p++), the value is incremented after the value is used.
The code for pre-increment is obvious. Increment the value, then return the updated value.
The code for post-increment is more subtle. Since the value of the operator function has to return the value BEFORE the value is incremented, the value is stored in a temporary variable. The real variable is incremented, then the temporary which contains the value before the increment is returned.
The int is simply a flag to the compiler to distinguish pre-increment (no flag) from post-increment operator functions. It serves no other purpose.
You got the same result in both cases because your program was not properly testing pre-increment and post-increment values.