13

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?

Junuxx
  • 14,011
  • 5
  • 41
  • 71
CodeCodeCode
  • 459
  • 1
  • 12
  • 21
  • A really smart compiler would remove all the code inside `main`, and thus realizing that (despite of the `virtual` declaration) no dynamic binding is being used. So it would not create the tables. Actually it would remove all the code. I see that https://stackoverflow.com/a/8932117/6607497 said the same. – U. Windl Jun 02 '23 at 10:11

6 Answers6

16

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.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • now if I modify my pgm like below: I remove the virtual keyword from the derived class. Now how many vtables will be created ? – CodeCodeCode Jan 19 '12 at 19:52
  • 2
    @Pal - No difference, the function is still virtual. – Bo Persson Jan 19 '12 at 21:22
  • @Bo Persson: function is virtual but, Will there be a vtable created for derived class also? – CodeCodeCode Jan 20 '12 at 08:34
  • 1
    @Pal - In this case it doesn't matter if you use the keyword or not, if the function is virtual in the base class it will be virtual in the derived class as well. And as it is two different functions, there will likely be two vtables as well. All this is implementation specific of course, and the compiler is allowed to optimize some special cases if it believes that is useful. – Bo Persson Jan 20 '12 at 12:11
  • @PalLoveCoding: A virtual table will be created for every class which itself declares a virtual function or inherits a virtual function.Rationale: If there is no virtual table for the derived class then there would be no-way to call the overidden derived class function in dynamic disptach. – Alok Save Jan 21 '12 at 04:44
  • 1
    @PalLoveCoding, every derived class will have a vtable even if it doesn't create or override any virtual functions. RTTI depends on it. – Mark Ransom May 18 '12 at 16:58
14

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

int main(){}

So, "none" is a possibility.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
9

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.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
6

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.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Thanks Als for the answer, now I got bit confused. please explaine me if I remove virtual keyword from class B(derived) then how many vtables will be created? – CodeCodeCode Jan 19 '12 at 20:04
  • @CodeCodeCode if you remove virtual keyword from class B(derived) then how many vtables will be created? the answer remains the same 2 one for base and derived. – Krishna Oza Mar 10 '14 at 07:05
  • 1
    @aloksave I read somewhere that since a base class has virtual function it will have a vptr and the same is inherited to the derived class but points to derived class vtable. Can you help on that. – Krishna Oza Mar 10 '14 at 07:09
4

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,

U. Windl
  • 3,480
  • 26
  • 54
Subi Suresh
  • 291
  • 1
  • 4
  • 10
1

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).

Faizan Khalid
  • 125
  • 2
  • 9