Suppose I have a diamond inheritance situation as follows:
class A{
public: virtual void foo(){};
};
class B: public virtual A{
public: virtual void foo(){};
};
class C: public virtual A{
public: virtual void foo(){};
};
class D: B, C{};
The last line yields a compilation error citing ambiguity. As I understand it, the problem is that the compiler doesn't know which foo to place in D's vtbl, but why would there even be a vtbl for D if it doesn't define any virtual functions of its own?
At compile time, the compiler can't know which code is going to be executed by the o->f() call since it doesn't know what o points to. Hence, you need something called a "virtual table" which is basically a table of function pointers.
Virtual base class in C++ Virtual classes are primarily used during multiple inheritance. To avoid, multiple instances of the same class being taken to the same class which later causes ambiguity, virtual classes are used.
A virtual table is a set of columns that have specific names and data types. These columns describe the format of data that flows into and out of an operator. Each operator port in a data flow operator has a virtual table that defines the structure of the data coming into or leaving that port.
A vtable is simply an array of function pointers. The slots in the array correspond to the methods of of a class. Since methods are fixed per class, we do not have to maintain an array of functions for each object. Rather, it suffices to declare one vtable array per class.
You're inheriting classes that contain virtual functions. Therefore, your class has virtual functions. It's as simple as that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With