Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the following class have a virtual table?

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?

like image 860
EpsilonVector Avatar asked Feb 09 '10 00:02

EpsilonVector


People also ask

Why do we need virtual table?

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.

Why do we use virtual class in C++?

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.

What is a virtual table in SQL?

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.

What is virtual table in Java?

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.


1 Answers

You're inheriting classes that contain virtual functions. Therefore, your class has virtual functions. It's as simple as that.

like image 103
Corey D Avatar answered Sep 20 '22 02:09

Corey D