Using the this Pointer to Enable Cascaded Function Calls
Another use of the
this pointer is to enable cascaded member-function calls—that is, invoking multiple functions in the same statement (as in line 12 of Fig. 9.26). The program of Figs. 9.24–9.26 modifies class
Time’s set functions
setSecond such that each returns a reference to a
Time object to enable cascaded member-function calls. Notice in Fig. 9.25 that the last statement in the body of each of these member functions returns
*this (lines 23, 34, 45 and 56) into a return type of
1 // Fig. 9.24: Time.h 2 // Cascading member function calls. 3 4 // Time class definition. 5 // Member functions defined in Time.cpp. ...