派生类、具体类的 vtable
如果我有一个基类,并从中派生出 10 个不同的具体派生类,那么每个具体派生类是否都会有一个不同的 vtable?
If I have one base class and I derive 10 different concrete derived classes from it then will each and every concrete derived class have a different vtable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
取决于您的派生类是否重写/声明任何虚拟方法。
Depends on whether your derived classes override/declare any virtual methods.
如果基类或所有派生类都有任何虚函数,那么通常是的。 它为什么如此重要?
如果两个类具有相同的虚拟函数集,则它们只能共享 vtable。
因此,如果派生类不重写任何虚函数,则它只能与基类共享 vtable。
派生类不能与任何其他派生类共享 vtable,除非它们都不会重写同一基类的任何函数,因为即使以相同的方式实现,一个派生类的成员函数与不同派生类的成员函数。
If the base class or all of the derived classes have any virtual functions, then yes, usually. Why is it important?
Two classes can only share a vtable if they have an indentical set of virtual functions.
So a derived class can only share a vtable with a base class if it doesn't override any virtual functions.
A derived class can't share a vtable with any other derived class unless they both don't override any functions of the same base class as - even if implemented in the same way - the member functions of one derived class are a different type from the member functions of a different derived class.