Class sizes with virtual inheritance in C++

前端 未结 5 1896
[愿得一人]
[愿得一人] 2021-01-02 20:48
#include

using namespace std;

class abc
{
    int a;
};
class xyz : public virtual abc
{
    int b;
};

int main()
{
    abc obj;
    xyz obj1;
            


        
5条回答
  •  粉色の甜心
    2021-01-02 21:07

    Non-virtual inheritance is just like object containment, given:

    struct Derived : Base
    

    It can be compiled to C++ just this way:

    struct Derived {
        Base __base;
        // other members
    
        // implementation of Derived-to-Base pointer conversion
        operator Base& () { return __base; }
    };
    

    Virtual inheritance is like adding a level of indirection, given

    struct Base
    struct L : virtual Base
    struct R : virtual Base
    struct Derived : L, R
    

    This can be compiled to C++ as

    // 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) {
        }
    };
    

    You get the idea...

    Note: I have not described the vtable pointer member. (It can be used instead of the Base&, to have smaller classes.)

提交回复
热议问题