I know that in case of virtual inheritance, a vptr is needed to access the base class members, so I looked at a program that involved virtual inheritance but I was amazed at the size of the class. The code is below:
#include<iostream>
using namespace std;
class Base
{
public:
};
class D1:public Base
{
};
class D2:virtual public Base
{
};
class DD: public D1, public D2
{
};
int main()
{
DD cObj;
cout<<sizeof(cObj)<<endl;
return 0;
}
For size of pointers = 4,
The size of class DD comes to be 8, even though class D1 doesn't use the usual Dreaded Diamond virtual inheritance.
If I make D1 use virtual inheritance the size comes to be 8 as well - which is OK(two vptr one for D1 and other for D2). But why 8 even without it?
Please can someone explain the reason behind it.
Thanks in advance.