Previous | Next | Table of Contents | Index | Program List | Copyright

15.5 System Structures: Dynamic Dispatching

Given the array of values in Program 15.9, we display the entire company just by looping through the array, dereferencing each pointer to obtain the value to display:

    FOR Which IN Company'Range LOOP
      Put(Company(Which).ALL);
      Text_IO.Put_Line(Item => "------------------------");
    END LOOP;
There is more to the Put in the above loop than meets the eye. Note that each value being displayed is of a different type, each of which has its own Put as defined in the three packages of Section 15.2. If we had used variant records, we would need a CASE to decide which variant to display. Here, the appropriate Put is selected, at execution time, automatically. This is called dynamic dispatching, and it is an extremely important technique in object-oriented programming. The correct Put is said to be dispatched.

Dispatching is closely related to primitive operations. In our example, Put is a primitive operation of Person. For Person and each type derived from Person, that is, each type in Person'Class, Put is inherited by default or, as in our situation, overridden. The five Puts have the same name and parameters differing only by the type within Person'Class. The correct Put can thus be dispatched.

We note that the values designated by Company(Which) could have been placed in Company by dynamic allocation instead of using ALIASED variables. In fact, the next section shows how to make Company fully dynamic.


Previous | Next | Table of Contents | Index | Program List | Copyright

Copyright © 1996 by Addison-Wesley Publishing Company, Inc.