Number of Virtual tables and Virtual Pointers in a C++ Program

13.7k views Asked by At

Let say we have below program:

class A
{     public:
      virtual fun(){};
};
class B:public A
{     public:
     virtual fun(){};
};
int main()
{
     A a1;
     B b1;
 }

My question is how many vtables and how many vptrs will be created, when we run this program?

6

There are 6 answers

6
Chris Dodd On BEST ANSWER

Its heavily implementation dependent, but generally you'll get one vtable object per class that has any virtual functions (classes with no virtual functions or bases don't need them), and one vptr per object of a class with a vtable (pointing at the class's vtable).

Things get more complex if you have multiple inheritance and virtual base classes -- which can be implemented many ways. Some implementations use an addition vtable per additional base class (so you end up with a vtable per base class per class), while others use a single vtable with extra info in it. This may result in needing multiple vptrs per object.

The virtual keyword in B is irrelevant -- if the function is virtual in the base class, it will be virtual in the derived classes regardless.

3
Alok Save On

Note that this is strictly implementation dependent.
C++ Standard does not talk of vptr or vtable, the virtual mechanism is left out as an implementation detail for compilers. So practically, compilers can implement it without using vptr or vtable.However, almost all known compilers implement it using vptr and vtable.

Given the above, to answer your question:

Each class will have its own virtual table.
While each object has its own virtual pointer.

0
Luchian Grigore On

Basically, 2. One for class A, one for class B (vftables) and 2 vfptrs, one for a1 and one for b1.

However, this is not standard mandated, so you could as well have none. (usually implementations use vftables, but its not mandated.

Note @R. Martinho Fernandes with optimizations on, you will have no objects created, so no vfptrs.

3
R. Martinho Fernandes On

This program can be optimized to be exactly like this one:

int main(){}

So, "none" is a possibility.

0
Faizan Khalid On

There will be 2 vtables, one for class A and one for class B. And there will be 3 vptrs, one in a1 and two in b1(one pointing to vtable of class A and other pointing to vtable of class B).

0
Subi Suresh On

Virual table will be created only if at least 1 virtual function is there in the Base class, which will be any way inherited to the derived classes. It doesn't matter even if you remove virtual keyword from derived class B because already u are having a virtual fun() in A. So the number of virtual tables will be 2 (as its per class basis) and number of virtual ptrs will also be 2, as its per object basis. VTABLE for A---v_ptr* , A::fun()

& VTABLE for B--- V_ptr*(which was inherited from A),B::fun()/* B have access to both A::fun & B's fun(), but since we mentioned A::fun() as virtual B's virtual table is filled with the most derived version of the function, fun(), which is nothing but B::fun(). Hope this clears your doubt,