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:

    There is a continual exchange of ideas between all minds of a generation. Journalists, popular novelists, illustrators, and cartoonists adapt the truths discovered by the powerful intellects for the multitude. It is like a spiritual flood, like a gush that pours into multiple cascades until it forms the great moving sheet of water that stands for the mentality of a period.
    Auguste Rodin (1849–1917)

    Every third year you shall bring out the full tithe of your produce for that year, and store it within your towns; the Levites, because they have no allotment or inheritance with you, as well as the resident aliens, the orphans, and the widows in your towns, may come and eat their fill so that the LORD your God may bless you in all the work that you undertake.
    Bible: Hebrew, Deuteronomy 14:28,29.