previous | start | next

## Operator Overloading

- There are actually two forms of the `++` and `--` operators.
  - A prefix form:

    >     ++x;

  - A postfix form:

    >     x++;

- Recall that `++x` evaluates to `x` after the increment, and `x++` evaluates to `x` before the increment.

  >     int i = 0;
  >     int j = 0;
  >     vector<double> s(10);
  >     double a = s[i++]; /* a is s[0], i is 1 */
  >     double b = s[++j]; /* b is s[1], j is 1 */

- In order for the compiler to distinguish between the two versions, the operators must have two different parameter lists.

  >     void operator++(Time& a) /* prefix operator */
  >     . . .

  >     void operator++(Time& a, int dummy) /* postfix operator */

- The `int dummy` parameter is not used inside the function, it merely serves to differentiate the two `operator++` functions.

previous | start | next