When overloading the postfix operator, I can do something simple like
Class Foo
{
private:
int someBS;
public:
//declaration of pre &postfix++
Citing C++reference here:
Prefix versions of the built-in operators return references and postfix versions return values, and typical user-defined overloads follow the pattern so that the user-defined operators can be used in the same manner as the built-ins.
Explaining this logically:
int a = 3;
int b = 0;
int c = a + ++b;
int d = a++ + b;
In the third line, ++b
gives you a reference to the updated value; in the fourth line, that's not possible: a
has to be increased, so a value-copy is made, and added to b
. That dictates different semantics for the operators.
In fact, just to avoid having another operator name:
The int parameter is a dummy parameter used to differentiate between prefix and postfix versions of the operators. When the user-defined postfix operator is called, the value passed in that parameter is always zero, although it may be changed by calling the operator using function call notation (e.g., a.operator++(2) or operator++(a, 2)).