The overloaded ++ and –
– operators present a dilemma because you want to be able to call
different functions depending on whether they appear before (prefix) or after
(postfix) the object they’re acting upon. The solution is simple, but
people sometimes find it a bit confusing at first. When the compiler sees, for
example, ++a (a pre-increment), it generates a call to
operator++(a); but when
it sees a++, it generates a call to operator++(a, int). That is,
the compiler differentiates between the two forms by making calls to different
overloaded functions. In OverloadingUnaryOperators.cpp for the member
function versions, if the compiler sees ++b, it generates a call to
B::operator++( ); if it sees b++ it calls
B::operator++(int).
All the user sees is that a different
function gets called for the prefix
and
postfix versions. Underneath, however, the two functions calls have different
signatures, so they link to two different function bodies. The compiler passes a
dummy constant value for the int argument (which is never given an
identifier because the value is never used) to generate the different signature
for the postfix
version.