As I know here is the way to overload the post-increment operator:
const MyClass& MyClass::operator++(int);
Why does it have int as an argument?
D&E, §11.5.3:
I conisdered the obvious solution, adding the keywords
prefix
andpostfix
to C++ [ ... ] However I received the usual howl of outrage from people who dislike new keywords. Several alternatives that did not involve new keywords were suggested. For example:
class Ptr_to_X { X ++operator(); // prefix ++ X operator++(); // postfix ++ };
or
class Ptr_to_X { X& operator++(); // postfix because it // returns a reference x operator++(); // prefix because it // doesn't return a reference };
I considered the former too cute and the latter too subtle. Finally I settled on:
class Ptr_to_X { X operator++(); // prefix: no argument X operator++(int); // postfix: because of the argument };
This may be too cute and too subtle, but it works, requires no new syntax, and has a logic to the madness. Other unary operators are prefix and take no arguments when defined as member functions. The "odd" and unused dummy
int
argument is used to indicate the odd postfix operators. In other words, in the postfix case,++
comes between the first (real) operand and the second (dummy) argument and is thus postfix.
These explanations are needed because the mechanism is unique, and therefore a bit of a wart. Given a choice I would probably have introduced theprefix
andpostfix
keywords, but that didn't appear feasible at the time.
This is to distinguish between prefix increment and postfix increment operators.
For completeness, this is set out in §13.5.7 in both the C++03 and the C++11 standards.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With