Middle+
How many tables of virtual functions will there be for two classes involved in inheritance?
sobes.tech AI
Answer from AI
The number of virtual function tables (vtable) depends on the type of inheritance and the presence of virtual functions in each class.
For simple inheritance:
- Base class without virtual functions: 0
- Base class with virtual functions: 1
- Derived class without its own virtual functions: 0 (when inheriting regular functions) or 1 (when inheriting virtual functions of the base class)
- Derived class with its own virtual functions (even if the base class did not have virtual functions): 1
When virtual functions are present in the base class and overridden in the derived class, or new virtual functions are added in the derived class, each class will have its own vtable.
In multiple inheritance, there can be more than one vtable per object if inheriting from multiple classes with virtual functions.
Thus, in the typical case of simple inheritance with virtual functions, two classes involved in inheritance can have from 0 to 2 virtual function tables per object, with each class having one vtable.