#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 Y
and Z
, g++ 4.4.5
produces two distinct virtual tables for Y
and Z
that have the same size.
Both tables point to the same x()
but point to different typeinfo
structures:
;=== Z's virtual table ===
_ZTV1Z:
.quad 0
.quad _ZTI1Z ; Z's type info
.quad _ZN1Z5xEv ; x()
_ZTI1Z:
; Z's type info (omitted for brevity)
;=== Y's virtual table ===
_ZTV1Y:
.quad 0
.quad _ZTI1Y ; Y's type info
.quad _ZN1Z5xEv ; x()
_ZTI1Y:
; Y's type info (omitted for brevity)