虚拟表C ++ [英] Virtual Table C++

查看:113
本文介绍了虚拟表C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了很多人写的虚拟表存在一个类中有一个虚拟函数声明了。



我的问题是,一个vtable仅存在于具有虚拟函数的类,或者对于从该类派生的类存在。



例如

  class Base {
public:
virtual void print(){cout<<Base Print\\\
;}
};
class Derived:public Base {
public:
void print(){cout<<Derived print\\\
;}
};

//从main.cpp
Base * b = new Derived;
b-> print();问题:如果没有类派生的vtable,那么输出将不会是派生打印 。所以IMO存在一个vtable为任何类有虚函数声明和类继承从该类。这是正确的吗?

解决方案

只有考虑虚拟函数特定的功能,当且仅当该派生类覆盖至少一个虚函数时,类才需要vtable 的单独版本。在您的示例中, Derived 覆盖虚函数 print 。由于 Derived 有自己的版本 print Derived vtable不同于 Base vtable中的。这通常需要一个单独的vtable Derived



如果 Derived 根本没有覆盖任何内容,它仍然是一个单独的多态类,为了使其虚拟函数正常工作,我们可以简单地为 Derived 重用 Base vtable。因此,技术上不需要为 Derived 单独的vtable。



然而,在实际实现,我们通常称为vtable的数据结构,通常还包含一些附加的类特定信息。这些额外的信息是类特定的,在大多数时候,它变得不可能在层次结构中的不同类之间共享vtables,即使他们使用相同的虚拟函数集。例如,在一些实现中,存储在每个多态对象中的vtable指针指向也存储关于该类的所谓的RTTI信息的数据结构。因此,在大多数(如果不是全部)实际实现中,每个多态类都有自己的vtable,即使存储在这些表中的虚函数指针恰好相同。


I read a lot of people writing "a virtual table exists for a class that has a virtual function declared in it".

My question is, does a vtable exists only for a class that has a virtual function or does it also exist for classes derived from that class.

e.g

class Base{
    public:
        virtual void print(){cout<<"Base Print\n";}
};
class Derived:public Base{
    public:
        void print(){cout<<"Derived print\n";}
};

//From main.cpp 
Base* b = new Derived;
b->print();

Question: Had there been no vtable for class derived then the output would not have been "derived print". So IMO there exists a vtable for any class that has virtual function declared and also in classes inheriting from that class. Is this correct ?

解决方案

As far as only virtual-function-specific functionality is considered, in a traditional approach to vtable implementation derived class would need a separate version of vtable if and only if that derived class overrides at least one virtual function. In your example, Derived overrides virtual function print. Since Derived has its own version of print, the corresponding entry in Derived vtable is different from that in Base vtable. This would normally necessitate a separate vtable for Derived.

If Derived didn't override anything at all, formally it still would be a separate polymorphic class, but in order to make its virtual functions work properly we could have simply reused Base vtable for Derived as well. So, technically there wouldn't be any need for a separate vtable for Derived.

However, in practical implementations, the data structure that we usually refer to as "vtable", often holds some additional class-specific information as well. That extra information is so class-specific that most of the time it becomes impossible to share vtables between different classes in hierarchy, even if they use the same set of virtual functions. For example, in some implementations the vtable pointer stored in each polymorphic object points to data structure that also stores so called "RTTI information" about the class. For this reason, in most (if not all) practical implementations each polymorphic class gets its own vtable, even if the virtual function pointers stored in those tables happen to be the same.

这篇关于虚拟表C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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