4

Virtual base class is a way of preventing multiple instances of a given class appearing in an inheritance hierarchy when using multiple inheritance . Then for the following classes

class level0 {
    int a;
    public :
    level0();
};

class level10:virtual public level0 {
    int b;
    public :
    level10();
};

class level11 :virtual public level0 {
    int c;
    public :
    level11();
};

class level2 :public level10,public level11 { 
    int d;
    public:
    level2();
};

I got following sizes of the classes

size of level0 4

size of level10 12

size of level11 12

size of level2 24

but when I removed virtual from inheritance of level10 and level11 I got following output

sizeof level0 4

sizeof level10 8

sizeof level11 8

sizeof level2 20

If virtual inheritance prevents multiple instances of a base class, then why size of classes is greater in case of virtual inheritance?

Elazar
  • 20,415
  • 4
  • 46
  • 67
  • You'll see the effect better if `level0` is larger. As it is, its size gets lost in the noise. Give it 3 or 4 `int` data members and things will be clearer. – Pete Becker Jun 24 '13 at 19:02
  • 2
    Duplicate of http://stackoverflow.com/questions/10541149/size-of-the-classes-in-case-of-virtual-inheritance – Marius Jun 24 '13 at 19:04
  • @Marius nope, that question has one `virtual`, this one has two. The answer uses that single `virtual` as part of the size description: it wouldn't work as an answer to the above. – Yakk - Adam Nevraumont Jun 24 '13 at 19:09
  • Ok then duplicate of http://stackoverflow.com/questions/9737847/multiple-inheritance-size-of-class-for-virtual-pointers – Marius Jun 24 '13 at 19:19
  • What architecture are you running this on? What is the size of a pointer vs. the size of an `int`? – David Rodríguez - dribeas Jun 24 '13 at 19:23
  • I hoping you're only asking this as an academic question. Because if this is actually important to you, then you're probably doing something wrong. – Edward Falk Jun 24 '13 at 19:24
  • Possible duplicate of [Size of the classes in case of virtual inheritance](https://stackoverflow.com/questions/10541149/size-of-the-classes-in-case-of-virtual-inheritance) – Superlokkus Apr 21 '19 at 13:28

2 Answers2

5

Because when using virtual inheritence, the compiler will create* a vtable to point to the correct offsets for the various classes, and a pointer to that vtable is stored along with the class.


  • "Will create" -- vtables are not dictated by the Standard, but the behaviors implied by virtual inheritence is. Most compilers use vtables to implement the functionality dictated by the Standard.
John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • Aren't the extra 4 bytes from a pointer to the virtual base object, whereas a vtable is a table of pointers to virtual functions' implementations. Since there are no virtual functions, the compiler shouldn't add vtables. – Suedocode Jun 24 '13 at 21:31
0

As you can see in this, it is very clear that if the virtual inheritance is used, the compiler adds one offset pointer to point base class rather than including the base class members in its own memory. That's the reason for increasing size. If it will be x64 bit m\c, pointer size will be 8. And the output will be followings

size of level0 4

size of level10 16

size of level11 16

size of level2 32

santosh kumar
  • 585
  • 4
  • 14