Number of Virtual tables and Virtual Pointers in a C++ Program
Asked Answered
H

6

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?

Hammond answered 19/1, 2012 at 19:24 Comment(1)
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://mcmap.net/q/842812/-number-of-virtual-tables-and-virtual-pointers-in-a-c-program said the same.Holston
S
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.

Scop answered 19/1, 2012 at 19:31 Comment(6)
now if I modify my pgm like below: I remove the virtual keyword from the derived class. Now how many vtables will be created ?Hammond
@Pal - No difference, the function is still virtual.Huffman
@Bo Persson: function is virtual but, Will there be a vtable created for derived class also?Hammond
@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.Huffman
@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.Francescafrancesco
@PalLoveCoding, every derived class will have a vtable even if it doesn't create or override any virtual functions. RTTI depends on it.Leckie
F
14

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

int main(){}

So, "none" is a possibility.

Faucet answered 19/1, 2012 at 19:32 Comment(3)
Not necesarrily, you could have 2 or 1000 (it's not mandated to be optimized out). But +1 for the thought :)Lozano
int main(){} is suffice to write a C++ program. no need of any other line of code. :)Hammond
Less funny, more completely accurate. Thanks for being pedantic. ;)Encrust
L
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.

Lozano answered 19/1, 2012 at 19:31 Comment(0)
F
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.

Francescafrancesco answered 19/1, 2012 at 19:30 Comment(3)
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?Hammond
@Hammond 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.Mycobacterium
@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.Mycobacterium
P
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,

Prothrombin answered 3/4, 2013 at 20:14 Comment(0)
N
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).

Neuropathy answered 12/1, 2018 at 5:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.