问题
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