Chapter 28. Increment Operators

The built-in prefix and postfix increment and decrement operators are great shorthand. Rather than writing

x = x + 1;

we can simply write

++x;

or

x++;

Of greater significance, especially since the advent of the STL, is that these operators can be overloaded for class types. The prefix increment and decrement operators are defined for classes by the following functions:

class X
{
  . . .
  X &operator ++();   // pre-increment
  X &operator —();   // pre-decrement
  X operator ++(int); // post-increment
  X operator —(int); // post-decrement
  . . .

Although there's nothing forcing you to do so, the intent is that the return types of the prefix and postfix forms are as shown above, that is, that the prefix forms return a non-const ...

Get Imperfect C++ Practical Solutions for Real-Life Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.