非仮想継承は、次の場合、オブジェクトの包含と同じです。
struct Derived : Base
次の方法で C++ にコンパイルできます。
struct Derived {
Base __base;
// other members
// implementation of Derived-to-Base pointer conversion
operator Base& () { return __base; }
};
仮想継承は、間接的なレベルを追加するようなものです。
struct Base
struct L : virtual Base
struct R : virtual Base
struct Derived : L, R
これは、次のように C++ にコンパイルできます。
// the type L& is translated to __L_subobject&
// the type L* is translated to __L_subobject*
// lvalue of L is translated to lvalue of __L_subobject
struct __L_subobject {
Base &__base_ref;
__L_subobject (Base &__base_ref)
: __base_ref(__base_ref) {
}
// other members
// pointer conversions:
operator Base& () { return __base_ref; }
};
// a definition of variable x of type L is translated to one with type __L_complete
// but any lvalue x is translated to x.__most_derived
// (it is assumed that rvalues have been already been translated to lvalues)
struct __L_complete {
// all virtual bases:
Base __base;
// derived partial subobjects:
__L_subobject __most_derived;
__L_complete () : __most_derived(__base) {}
};
// ... same for R ...
struct __Derived_subobject {
__L_subobject __L;
__R_subobject __R;
// other members ...
__Derived_subobject (Base &__base_ref)
: __L(__base_ref),
__R(__base_ref) {
}
// pointer conversions:
operator Base& () { return __L.operator Base& (); }
operator __L_subobject& () { return __L; }
operator __R_subobject& () { return __R; }
};
struct __Derived_complete {
// all virtual bases:
Base __base;
// derived partial subobjects:
__Derived_subobject __most_derived;
__Derived_complete () :__most_derived(__base) {
}
};
あなたはアイデアを得る...
注: vtable ポインター メンバーについては説明していません。Base&
(より小さなクラスを持つために、の代わりに使用できます。)