#include <iostream>
using namespace std;
class Z
{
public:
int a;
virtual void x () {}
};
class Y : public Z
{
public:
int a;
};
int main()
{
cout << "\nZ: " << sizeof (Z);
cout << "\nY: " << sizeof (Y);
}
Because Y inherits Z, so it will also have a virtual table. Fine. But, it doesn’t have any virtual functions, so what will be the contents of the virtual table of Y?
Will it be empty?
This is entirely compiler-dependent. When I force instantiation of
YandZ,g++ 4.4.5produces two distinct virtual tables forYandZthat have the same size.Both tables point to the same
x()but point to differenttypeinfostructures: