Overloading the Prefix Increment Operator

Suppose that we want to add 1 to the day in Date object d1. When the compiler sees the preincrementing expression ++d1, the compiler generates the member-function call

d1.operator++()

The prototype for this operator member function would be

Date &operator++();

If the prefix increment operator is implemented as a non-member function, then, when the compiler sees the expression ++d1, the compiler generates the function call

operator++( d1 )

The prototype for this non-member operator function would be declared as

Date &operator++( Date & );

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.