Overloading the prefix increment operator is straightforward. The prefix increment operator (defined in Fig. 10.7, lines 41–45) calls utility function helpIncrement
(defined in Fig. 10.7, lines 87–104) to increment the date. This function deals with “wraparounds” or “carries” that occur when we increment the last day of the month. These carries require incrementing the month. If the month is already 12, then the year must also be incremented and the month must be set to 1. Function helpIncrement
uses function endOfMonth
to determine whether the end of a month has been reached and increment the day correctly.
The overloaded prefix increment operator returns a reference to the current Date
object (i.e., the one that was just incremented). This occurs because the current object, *this
, is returned as a Date &
. This enables a preincremented Date
object to be used as an lvalue, which is how the built-in prefix increment operator works for fundamental types.