Tag: 虚函数

为什么派生类没有vtable指针而是使用基类的vtable?

我感兴趣的是在纯C中实现虚函数。这里是一个实现的例子 。 然后通过指向基类的虚函数表的指针来实现派生类。 为什么派生类没有vtable指针而是使用基类的vtable。 也许是因为它们是相同的偏移量? void myClassDerived_ctor(struct myClassDerived *this) { myClassBase_ctor(&this->base); this->base.vtable = (void*)&myClassDerived_vtable + sizeof(void*); // used vtable of the base class }