Now recall that in ordinary_capacity_function
, the parameter,
r
is a railroad_car
parameter. Accordingly, the argument
is copied into a chunk of memory suited to a railroad_car
object.
Suppose, however, that the argument is actually a box_car
object,
identified by a dereferenced railroad-car pointer. Unfortunately, at
compile time, the C++ compiler cannot anticipate that your program will
hand such an argument to ordinary_capacity_function
. Accordingly,
only the railroad-car portion of the box_car
object is
copied:
Memory reserved for Memory reserved for r, box_car argument a railroad_car parameter | | v v *-----* Copy *-----* | | -------------> | | |-----| |-----| | | | | |-----| *-----* | | | |-----| | | | | box_car-specific portion not copied |-----| | | | | *-----*
The net result is that only the chunk of memory that holds a
railroad_car
object is visible to the C++ mechanism that is
supposed to figure out what version of the capacity
virtual member
function to use at run time. Because the railroad-car version of
capacity
returns 0.0
, you end up with 0.0
filling an
entire column of the report displayed by the analyze_train
program.