There are two other important reasons to use call-by-reference parameters.
Suppose that you have an array of box_car
objects,
rather than railroad_car
objects. Then, you could define
floor_space_function
as follows:
double floor_space_function (box_car b) { return b.width * b.length; }
Equipped with the floor_space_function
, you could incorporate it
into a program that uses your array of box_car
objects:
box_car *box_car_array[100]; ... main ( ) { ... for (n = 0; n < car_count; ++n) { // Display floor space and terminate the line: cout << floor_space_function (*box_car_array[n]) << endl; } }
This program will run
faster if you define
floor_space_function
with a call-by-reference parameter to avoid
copying the box_car
object:
double floor_space_function (box_car& b) { return b.width * b.length; }