Page 304 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 304
9.5 Procedure Calls and Arguments 281
C++ generally has similar operators and implements them in assembly language in
similar ways. However, C++ has a calling mechanism, where a member function is
designated virtual, that permits run-time substitutions of one class and its function
members for another class and its function members. If we do not insert the word
virtual in front of a function member in the class declaration, then the function is
directly called by a JSR or BSR instruction, like C procedures discussed above.
If a function member is declared virtual, then to call it, we look its address up in
a viable associated with the class, as is shown on the right side of Figure 9.16. This
table is used because generally a lot of objects of the same class might be declared or
blessed, and they might have many virtual function members. For instance there could be
stacks for input and for output and stacks holding temporary results in the program. A
single table holds the function member addresses for all of a class's objects in one place.
Suppose Q is a pointer to an object, and the object stores data members in the block
pointed to by Q. The hidden pointer, at location Q, points to the jump table. Then, data
members are easily accessed by the pointer Q, and virtual function members are almost
as easily accessed by means of a pointer to a pointer.
The operator new blesses a pointer to an object. A subroutine allocate will
return a location where the data members and the hidden pointer can be stored in index
register X. Then if ctbl is the jump table for the class Cstack, the statement Sptr =
new Cstack; is implemented as
JSR allocate ; return value in accumulator D is a pointer to object data
STX Sptr ; save address in pointer to the object
MOW #Ctbl, 0, X ; put the class Cstack jump table address in the hidden pointer
If new blesses a pointer to an object to make it an object of a different class,
1 stack, then if Itbl is the jump table for the class Istack, the statement Sptr =
new Istack; is implemented as
JSR allocate ; return value in accumulator D is a pointer to object data
STX Sptr ; save address in pointer to the object
MOW #tbl 1, 0, X ; put class CharQueue's jump table address in the hidden pointer
Figure 9.16. An Object and Its Pointers