Virtual Method Table - Multiple Inheritance and Thunks

Multiple Inheritance and Thunks

The g++ compiler implements the multiple inheritance of the classes B1 and B2 in class D using two virtual method tables, one for each base class. (There are other ways to implement multiple inheritance, but this is the most common.) This leads to the necessity for "pointer fixups", also called thunks, when casting.

Consider the following C++ code:

D *d = new D; B1 *b1 = static_cast(d); B2 *b2 = static_cast(d);

While d and b1 will point to the same memory location after execution of this code, b2 will point to the location d+8 (eight bytes beyond the memory location of d). Thus, b2 points to the region within d which "looks like" an instance of B2, i.e., has the same memory layout as an instance of B2.

Read more about this topic:  Virtual Method Table

Famous quotes containing the words multiple and/or inheritance:

    ... the generation of the 20’s was truly secular in that it still knew its theology and its varieties of religious experience. We are post-secular, inventing new faiths, without any sense of organizing truths. The truths we accept are so multiple that honesty becomes little more than a strategy by which you manage your tendencies toward duplicity.
    Ann Douglas (b. 1942)

    I call it our collective inheritance of isolation. We inherit isolation in the bones of our lives. It is passed on to us as sure as the shape of our noses and the length of our legs. When we are young, we are taught to keep to ourselves for reasons we may not yet understand. As we grow up we become the “men who never cry” and the “women who never complain.” We become another generation of people expected not to bother others with our problems.
    Paula C. Lowe (20th century)