How many v_tables exist for a class that inherits twice

struct A 
{
    virtual void f1() {}
};

struct B: public A {};

struct C: public A {};

struct D: public B,public C {};

int main()
{
    cout<<sizeof(A)<<" "<<sizeof(B)<<" "<<sizeof(C)<<" "<<sizeof(D)<<endl;
    cout<<"_______"<<endl;
    return 0;
}

      

  • Result on my system (c9.o): 8 8 8 16

    Does this mean D has 2 virtual pointers to 2 virtual tables? If so, where are the pointers?

  • If we remove the fucntion f1

    of struct A

    , the output will be as follows: 1 8 8 16

    . The same question is being asked here.

  • If we do not remove f1

    , and now B

    and C

    inherit the virtual machine from A

    the result: 8 8 8 16

    . The same question is being asked here.

+3


source to share





All Articles