< previous page page_272 next page >

Page 272
10905-0272a.gif
FIGURE 17.1
The
Dog object after it is created.
When a virtual function is created in an object, the object must keep track of that function. Many compilers build a virtual function table, called a v-table. One of these is kept for each type, and each object of that type keeps a virtual table pointer (called a vptr or v-pointer), which points to that table.
Although implementations vary, all compilers must accomplish the same thing, so you won't be too wrong with this description.
Each object's vptr points to the v-table that, in turn, has a pointer to each of the virtual member functions. When the Mammal part of the Dog is created, the vptr is initialized to point to the correct part of the v-table, as shown in Figure 17.2.
10905-0272b.gif
FIGURE 17.2
The v-table of a
Mammal.
When the Dog constructor is called and the Dog part of this object is added, the vptr is adjusted to point to the virtual function overrides (if any) in the Dog object, as illustrated in Figure 17.3.
When a pointer to a Mammal is used, the vptr continues to point to the correct function, depending on the real type of the object. Thus, when Speak() is invoked, the correct function is invoked.

 
< previous page page_272 next page >

If you like this book, buy it!