gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:46:51 24 4
gpt4 key购买 nike

我对纯 C 中虚函数的实现很感兴趣。这里是 example of the implementation .然后通过指向基类虚函数表的指针实现派生类。为什么派生类没有 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
}

最佳答案

必须使用基类的虚表。重点是它看起来就像一个基类,但在 vtable 中有不同条目。因此它是多态的不同行为。

关于c++ - 为什么派生类没有vtable指针而是使用基类的vtable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19588993/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com