vtable in case of virtual inheritance [duplicate]

徘徊边缘 提交于 2019-12-12 21:03:58

问题


What is the use of vtable (or why is vtable required ) in case of virtual inheritance ? what does this vtable points to in this case.

example:

class A
{
    void show()
    { }
};

class B : virtual A
{
    void disp()
    { }
};

In the above example the size of class B is 8 bytes. which means class B has vptr pointing to a Vtable. What does this vtable point to .


回答1:


A vtable is the most common way of implementing the virtual keyword in C++ -- any class that uses the virtual keyword will have a vtable created for it and every instance of that class will contain a pointer to that (single) vtable. The vtable contains information on the dynamic class of the object (to support dynamic_cast and typeinfo) as well as information as to where virtual base classes and functions of the class are located.

In this specific case, the vtable for B will likely contain just dynamic class info, as A has no data members or virtual functions.



来源:https://stackoverflow.com/questions/44989780/vtable-in-case-of-virtual-inheritance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!