为什么派生类没有vtable指针而使用基类的vtable? [英] Why derived class does not have the vtable pointer and used instead vtable of the base class?

查看:183
本文介绍了为什么派生类没有vtable指针而使用基类的vtable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对纯C中的虚拟函数的实现感兴趣。这里有一个实现示例。然后通过指向基类的虚函数表的派生类的实现。为什么派生类没有vtable指针,而是使用基类的vtable。也许是因为他们是相同的偏移量?

I am interested in the implementation of a virtual function in pure C. Here an example of the implementation. Then the implementation of the derived class through a pointer to the virtual functions table of the base class. Why derived class does not have the vtable pointer and used instead vtable of the base class. Maybe because they are the same offset ?

void myClassDerived_ctor(struct myClassDerived *this)
{
    myClassBase_ctor(&this->base);
    this->base.vtable = (void*)&myClassDerived_vtable + sizeof(void*); // used vtable of the base class
}


推荐答案

使用基类的vtable。整个点是看起来像基类,但在vtable中有不同的条目。因此,它是多态的不同的行为。

It has to use the base class's vtable. The whole point is it looks just like a base class, but has different entries in the vtable. Hence it's polymorphicly different behaviour.

这篇关于为什么派生类没有vtable指针而使用基类的vtable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆