2
class A1
{
public:
    A1()
    {
        cout << "Inside Constructor A1." << endl;

    }
    virtual void fA1()
    {
        cout << "fA1() is called" << endl;
    }
};

class B1
{
public:
    B1()
    {
        cout << "Inside Constructor B1." << endl;
    }
    virtual void fB1()
    {
        cout << "fB2() is called" << endl;
    }

};

class C1: public B1, public A1
{
public:
    C1()
    {
        cout << "Inside Constructor C1" << endl;
    }
    virtual void fc1()
    {
        cout << "fc1() is called " << endl;
    }
};

cout << "Size of A1 class : " << sizeof(A1) << endl; //Gives 4

cout << "size of B1 Class : " << sizeof(B1) << endl;//4

cout << "size of C1 class : " << sizeof(C1) << endl;//8

why it happens? if vptr of class A and B are inherited into B(size of class C is 8) then, what about vptr of class c ?i.e, size of c should be 16. How the vtable is structured for class c ? is there any possible to see vtable in visual studio.

mch
  • 9,424
  • 2
  • 28
  • 42
Gopi Tyro
  • 21
  • 1
  • 5
  • vtables are not defined by the standard. It is likely but not certain that most implementations follow Lippman, *Inside The C++ Object Model,* Addison Wesley. – user207421 Mar 27 '17 at 08:50
  • 1
    The sizes of the types have very little to do with vtables. Vtables are generated *per class*, not per object, and the size of a type is a property of *objects* of that type. What you're seeing is much more closely related to base layout optimizations, or the lack thereof. – Kerrek SB Mar 27 '17 at 08:52
  • @Kerrek SB please, explain structure of vtable of calss C1 – Gopi Tyro Mar 27 '17 at 08:56
  • Class `C` will contain two virtual tables. For more information read http://shaharmike.com/cpp/vtable-part2/ – Zefick Mar 27 '17 at 08:57

0 Answers0