#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);
}
因为Y继承了Z,所以它也会有一个虚拟表。美好的。但是,它没有任何虚函数,那么Y的虚表的内容是什么?
会是空的吗?
这完全取决于编译器。当我强制实例化时Y
and Z
, g++ 4.4.5
生成两个不同的虚拟表Y
and Z
具有相同的尺寸。
两个表都指向同一个x()
但指向不同typeinfo
结构:
;=== 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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)