为什么派生类没有虚函数表指针代替虚函数表的基类的? [英] Why derived class does not have the vtable pointer and used instead vtable of the base class?

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

问题描述

我感兴趣的是纯C的虚函数的实现下面的执行情况进行例子。然后派生类的通过一个指向基类的虚拟函数表的执行。为什么派生类没有虚函数表的指针,而使用虚函数表的基类。也许是因为它们是相同的偏移

 无效myClassDerived_ctor(结构myClassDerived *本)
{
    myClassBase_ctor(安培;这 - >基地);
    这 - > base.vtable =(无效*)及myClassDerived_vtable +的sizeof(void *的); //基类的虚函数表使用
}


解决方案

它的的使用基类的虚函数表。整点是它的看起来的就像一个基类,但的不同的的vtable中的条目。因此,它是polymorphicly的不同的的行为。

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
}

解决方案

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.

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

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