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_castWhile 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:
“Combining paid employment with marriage and motherhood creates safeguards for emotional well-being. Nothing is certain in life, but generally the chances of happiness are greater if one has multiple areas of interest and involvement. To juggle is to diminish the risk of depression, anxiety, and unhappiness.”
—Faye J. Crosby (20th century)
“A child is born with the potential ability to learn Chinese or Swahili, play a kazoo, climb a tree, make a strudel or a birdhouse, take pleasure in finding the coordinates of a star. Genetic inheritance determines a childs abilities and weaknesses. But those who raise a child call forth from that matrix the traits and talents they consider important.”
—Emilie Buchwald (20th century)