user1886376
user1886376

Reputation:

What will be found in the table of virtual functions of the class child. It is empty or not created at all?

There are two classes, the second class will inherit the first. The base class has virtual functions and in the derived no virtual functions. What will be found in the table of virtual functions of the class child. It is empty or not created at all?

class base
{
private:
int m_d;
public:
virtual void foo();
virtual void bar();
};

class derived : public base
{
int m_b;
void baz();
};

Upvotes: 1

Views: 93

Answers (2)

Kerrek SB
Kerrek SB

Reputation: 477514

The vtable is an implementation detail of C++ compilers, so there is no standard answer. It would make sense with respect to minimizing indirections that each vtable contains the full set of virtual functions of all its bases, but again, this is an implementation detail.

Upvotes: 3

sharptooth
sharptooth

Reputation: 170539

The typical solution is to have a vtable per class, so the derived class will have a separate vtable identical to the vtable of the base class. This is done so that it is possible to find which class it is (as when doing dynamic_cast) just by having a vpointer value.

Upvotes: 1

Related Questions