Home Segments Index Top Previous Next

506: Sidetrip

The implicit-argument mechanism makes use of what is called the this pointer. Every member function has an implied parameter, named this, whose value is a pointer to the member function's class-object argument.

When you want to refer to the class argument of a member function, you could write down this along with a dereferencing asterisk. Thus, you could define the display_capacity function inside the box_car class definition as follows:

                                   Argument is the same as the 
                                argument handed to display_capacity 
                                              | 
                                              v 
                                           ------- 
virtual void display_capacity ( ) {cout << (*this).volume ( );} 

Alternatively, you can use the class-pointer operator to go through the pointer:

virtual void display_capacity ( ) {cout << this -> volume ( );} 

Fortunately, C++ allows you to leave out the first argument entirely:

virtual void display_capacity ( ) {cout << volume ( );} 

Thus, the real result of leaving out an explicit class-object argument is that the dereferenced this pointer is used to supply a default class-object argument.