单继承很容易实现。例如,在 C 中,继承可以模拟为:
struct Base { int a; }
struct Descendant { Base parent; int b; }
但是对于多重继承,编译器必须在新构造的类中安排多个父级。它是如何完成的?
我看到的问题是:父母应该安排在AB还是BA,或者甚至其他方式?然后,如果我进行演员表:
SecondBase * base = (SecondBase *) &object_with_base1_and_base2_parents;
编译器必须考虑是否改变原始指针。虚拟机也需要类似的棘手事情。