12.5 - The Virtual Table - Learn C++
12.5 - The Virtual Table - Learn C++
To implement virtual functions, C++ uses a special form of late binding known as the virtual table. The virtual table is a
lookup table of functions used to resolve function calls in a dynamic/late binding manner. The virtual table sometimes
goes by other names, such as “vtable”, “virtual function table”, “virtual method table”, or “dispatch table”.
Because knowing how the virtual table works is not necessary to use virtual functions, this section can be considered
optional reading.
The virtual table is actually quite simple, though it’s a little complex to describe in words. First, every class that uses
virtual functions (or is derived from a class that uses virtual functions) is given its own virtual table. This table is simply a
static array that the compiler sets up at compile time. A virtual table contains one entry for each virtual function that can
be called by objects of the class. Each entry in this table is simply a function pointer that points to the most-derived
function accessible by that class.
Second, the compiler also adds a hidden pointer to the base class, which we will call *__vptr. *__vptr is set (automatically)
when a class instance is created so that it points to the virtual table for that class. Unlike the *this pointer, which is actually
a function parameter used by the compiler to resolve self-references, *__vptr is a real pointer. Consequently, it makes
each class object allocated bigger by the size of one pointer. It also means that *__vptr is inherited by derived classes,
which is important.
By now, you’re probably confused as to how these things all fit together, so let’s take a look at a simple example:
1 class Base
2 {
3 public:
4 virtual void function1() {};
5 virtual void function2() {};
6 };
7
8 class D1: public Base
9 {
10 public:
11 virtual void function1() {};
12 };
13
14 class D2: public Base
15 {
16 public:
17 virtual void function2() {};
18 };
Because there are 3 classes here, the compiler will set up 3 virtual tables: one for Base, one for D1, and one for D2.
The compiler also adds a hidden pointer to the most base class that uses virtual functions. Although the compiler does
this automatically, we’ll put it in the next example just to show where it’s added:
1 class Base
2 {
3 public:
4 FunctionPointer *__vptr;
5 virtual void function1() {};
6 virtual void function2() {};
7 };
8
9 class D1: public Base
https://www.learncpp.com/cpp-tutorial/125-the-virtual-table/ 1/15
9/17/2019 12.5 — The virtual table | Learn C++
10 {
11 public:
12 virtual void function1() {};
13 };
14
15 class D2: public Base
16 {
17 public:
18 virtual void function2() {};
19 };
When a class object is created, *__vptr is set to point to the virtual table for that class. For example, when a object of type
Base is created, *__vptr is set to point to the virtual table for Base. When objects of type D1 or D2 are constructed,
*__vptr is set to point to the virtual table for D1 or D2 respectively.
Now, let’s talk about how these virtual tables are filled out. Because there are only two virtual functions here, each virtual
table will have two entries (one for function1(), and one for function2()). Remember that when these virtual tables are filled
out, each entry is filled out with the most-derived function an object of that class type can call.
The virtual table for Base objects is simple. An object of type Base can only access the members of Base. Base has no
access to D1 or D2 functions. Consequently, the entry for function1 points to Base::function1(), and the entry for function2
points to Base::function2().
The virtual table for D1 is slightly more complex. An object of type D1 can access members of both D1 and Base.
However, D1 has overridden function1(), making D1::function1() more derived than Base::function1(). Consequently, the
entry for function1 points to D1::function1(). D1 hasn’t overridden function2(), so the entry for function2 will point to
Base::function2().
The virtual table for D2 is similar to D1, except the entry for function1 points to Base::function1(), and the entry for
function2 points to D2::function2().
https://www.learncpp.com/cpp-tutorial/125-the-virtual-table/ 2/15
9/17/2019 12.5 — The virtual table | Learn C++
Although this diagram is kind of crazy looking, it’s really quite simple: the *__vptr in each class points to the virtual table
for that class. The entries in the virtual table point to the most-derived version of the function objects of that class are
allowed to call.
1 int main()
2 {
3 D1 d1;
4 }
1 int main()
2 {
3 D1 d1;
4 Base *dPtr = &d1;
5 }
Note that because dPtr is a base pointer, it only points to the Base portion of d1. However, also note that *__vptr is in the
Base portion of the class, so dPtr has access to this pointer. Finally, note that dPtr->__vptr points to the D1 virtual table!
Consequently, even though dPtr is of type Base, it still has access to D1’s virtual table (through __vptr).
1 int main()
2 {
3 D1 d1;
4 Base *dPtr = &d1;
https://www.learncpp.com/cpp-tutorial/125-the-virtual-table/ 3/15
9/17/2019 12.5 — The virtual table | Learn C++
5 dPtr->function1();
6 }
First, the program recognizes that function1() is a virtual function. Second, the program uses dPtr->__vptr to get to D1’s
virtual table. Third, it looks up which version of function1() to call in D1’s virtual table. This has been set to D1::function1().
Therefore, dPtr->function1() resolves to D1::function1()!
Now, you might be saying, “But what if dPtr really pointed to a Base object instead of a D1 object. Would it still call
D1::function1()?”. The answer is no.
1 int main()
2 {
3 Base b;
4 Base *bPtr = &b;
5 bPtr->function1();
6 }
In this case, when b is created, __vptr points to Base’s virtual table, not D1’s virtual table. Consequently, bPtr->__vptr will
also be pointing to Base’s virtual table. Base’s virtual table entry for function1() points to Base::function1(). Thus, bPtr-
>function1() resolves to Base::function1(), which is the most-derived version of function1() that a Base object should be
able to call.
By using these tables, the compiler and program are able to ensure function calls resolve to the appropriate virtual
function, even if you’re only using a pointer or reference to a base class!
Calling a virtual function is slower than calling a non-virtual function for a couple of reasons: First, we have to use the
*__vptr to get to the appropriate virtual table. Second, we have to index the virtual table to find the correct function to call.
Only then can we call the function. As a result, we have to do 3 operations to find the function to call, as opposed to 2
operations for a normal indirect function call, or one operation for a direct function call. However, with modern computers,
this added time is usually fairly insignificant.
Also as a reminder, any class that uses virtual functions has a __vptr, and thus each object of that class will be bigger by
one pointer. Virtual functions are powerful, but they do have a performance cost.
12.6 -- Pure virtual functions, abstract base classes, and interface classes
Index
« Older Comments 1 … 3 4 5
Alex2
August 23, 2019 at 12:22 am · Reply
https://www.learncpp.com/cpp-tutorial/125-the-virtual-table/ 4/15
9/17/2019 12.5 — The virtual table | Learn C++
stephane
May 23, 2019 at 3:48 pm · Reply
Hi !
1 class Base {
2 public:
3 virtual void foo() {};
4 };
5
6 class Derived: public Base {
7 public:
8 virtual void bar() {};
9 };
10
11 int main() {
12
13 Derived B;
14 Base* pA = &B;
15
16 // So here, if pA->v_ptr points to Derived::v_table, then its v_table should
17 // contain bar since it is declared virtual. So what is the process before
18 // going in v_table to fetch the function ? does the compilator check if
19 // the function called does exist in the Base::v_table before calling the one
20 // in Derived::v_table ? thank !
21 pA->bar();
22 }
Alex
May 23, 2019 at 8:45 pm · Reply
Base's __vtable::bar points to Base::bar since Base (non-derived) objects calling bar() should get the Base
version of the function.
yankee
July 7, 2019 at 12:59 pm · Reply
Hi Alex,
For the above example code, because function bar() is only initialized in the Derived class, Base class
pointer should have no access to bar(). Right?
I have a question. Is the vtable of each class also a hidden public member or a private member?
You explained that vtable is created for each class, which implies that it is individual to each class and
turns out to be a private member. So basically, the compiler collects all virtual functions, including the
inherited, overloaded, and firstly initialized, for each class and store the pointers of these functions to the
vtable. Is this correct?
Thanks.
nascardriver
July 8, 2019 at 3:18 am · Reply
https://www.learncpp.com/cpp-tutorial/125-the-virtual-table/ 5/15
9/17/2019 12.5 — The virtual table | Learn C++
The vtable is not a class member, only a pointer to the vtable is stored in each
instance. That way the same vtable can be used across several instances of a class.
The vtable pointer is neither public nor private, you can't access it at all (Without trickery).
Mohit
June 9, 2019 at 8:16 pm · Reply
1 #include <iostream>
2
3 using namespace std;
4
5 class Base
6 {
7 private:
8 /* data */
9 public:
10 Base(/* args */){};
11 ~Base(){};
12
13 //int func(int i) same answer regardless of virtual
14 virtual int func(int i)
15 {
16 cout << "Base func()" << endl;
17 return i+1;
18 }
19 };
20
21 class Derived : public Base
22 {
23 public:
24 Derived(/* args */){};
25 ~Derived(){};
26
27 virtual double func(double d)
28 {
29 cout << "Derived func()" << endl;
30 return d+1.3;
31 }
32 };
33
34 int main() {
35 Base* bd = new Derived();
36 // Derived vtable contains 2 entries: int func(int) & double func(double). Righ
37 t?
38 //bd--->derived vptr--->derived vtable--->Base::func()
39 // since parameter = 2 = integer
40 cout << bd->func(2) << endl;
41
42 //bd--->derived vptr--->derived vtable--->derived::func()
43 // since parameter = double = 2.3
44 // How is the above understanding incorrect?
45
46 cout << bd->func(2.3) << endl;
47 return 0;
48 }
49
https://www.learncpp.com/cpp-tutorial/125-the-virtual-table/ 6/15
9/17/2019 12.5 — The virtual table | Learn C++
50 // My expectation:
51 // Base func()
52 // 3
53 // Derived func()
54 // 3.6
55
56 // Actual output:
57 // Base func()
58 // 3
59 // Base func()
// 3
nascardriver
June 10, 2019 at 12:43 am · Reply
> Derived vtable contains 2 entries: int func(int) & double func(double). Right?
Right.
Problem 1
`bd` is a `Base *`, you can only call functions that are part of `Base`. There is not `func(double)` is base,
line 45 calls `func(int)`. Enable compiler warnings (warning: implicit conversion from 'double' to 'int'
changes value from 2.3 to 2).
Problem 2
Say you change line 35 to
You might expect line 39 to print `3`, but it won't. It calls `func(double)`. Re-using a function name in a
derived class, without overriding a parent function, hides all parent functions with the same name. This
produces a compiler warning (warning: 'Derived::func' hides overloaded virtual function).
If you want both functions to be visible at the same time, you need to be `using` the parent function.
Arto Mihalache
May 8, 2019 at 7:15 pm · Reply
Hi, I wanted to say that I really appreciate these types of sections dedicated to explaining the internal
implementations. As someone who's never really worked in a compiled language, its just very cool to me
how many optimizations can be done before run-time, so thank you again very much for this :)
Anthony
May 4, 2019 at 8:00 am · Reply
1 class Base {
2 public:
3 virtual void function1() { std::cout << "Base func1()\n"; };
4 virtual void function2() { std::cout << "Base func2()\n"; };
https://www.learncpp.com/cpp-tutorial/125-the-virtual-table/ 7/15
9/17/2019 12.5 — The virtual table | Learn C++
5 };
6
7 class D1: public Base {
8 public:
9 virtual void function2() { std::cout << "D1 func2()\n"; };
10 };
11
12 class D2: public D1 {
13 public:
14 virtual void function1() {std::cout << "D2 func1()\n"; };
15 };
16
17 class D3: public D2 {
18 public:
19 virtual void function2() { std::cout << "D3 func2()\n"; };
20 virtual void function3() { std::cout << "D3 func3()\n"; };
21 };
22
23 class D4: public D3 {
24 public:
25 virtual void function1() { std::cout << "D4 func1()\n"; };
26 virtual void function3() { std::cout << "D4 func3()\n"; };
27 };
28
29 int main() {
30 D4 d;
31 D2 *dPtr = &d;
32 dPtr->function1(); // routed via the vtable to jump to the most derived function
33 }
As can be seen, D3 introduces a new virtual function, @function3(), in the middle of the chain of inheritance. I am
wondering what is happening with *--vptr when this happens. D3 is a 'kind of' a new base class now, but there can't be
two *--vpr's now. What happens here?
Alex
May 6, 2019 at 2:03 pm · Reply
Since d is a D4, d's virtual pointer points to the virtual table for class D4, which includes function3
(inherited from D3).
Anthony
May 3, 2019 at 4:39 pm · Reply
Hi Alex,
I understand that any given class only has one vtable (a static array). But why does every object of that class have to
have its own *__vptr pointer? Surely a single pointer (with static duration) would do?
-- Anthony
nascardriver
May 4, 2019 at 1:05 am · Reply
But how does the object know where to find the static pointer?
https://www.learncpp.com/cpp-tutorial/125-the-virtual-table/ 8/15
9/17/2019 12.5 — The virtual table | Learn C++
1
2 Base *pBase{ getChild() }; // @getChild returns some class that inherits from @Base.
pBase->function(); // @function was overridden by the child.
There's no information about the child here. The compiler cannot know which function @function is at compile-
time. The only ways to resolve @function is to either store a pointer to the vtable in the child object, or store the
child's type (eg. id) in the child object and look up the corresponding vtable.
Anthony
May 4, 2019 at 1:43 am · Reply
Mehul
March 19, 2019 at 7:50 pm · Reply
Hi Alex,
Can you please let me know when are the vtable loaded with the function addresses at compile time or at run time. If it
is at runtime then is it when the instance of a class created else vtables are loaded same as static variables before the
main function starts ?
Are the vtable member variables, I think they are not. So are this stored in global scope. ?
Again Thanks is a small word for such a detailed tutorial it always helps me making my concepts stronger
Regards,
Mehul.
Alex
March 19, 2019 at 9:06 pm · Reply
The vtables are set up at compile time (and hence are static rather than dynamic). The vtable
pointer is a member pointer, and it's pointed at the correct vtable at runtime (upon object
initialization).
Anand
March 11, 2019 at 1:24 am · Reply
Where vtable will be stored(which segment)? Suppose if I have multiple objects of the same derived
class, what is the behaviour of Vtable? means, storage, number of instances in total.
Alex
March 11, 2019 at 10:11 pm · Reply
sowmyashree
February 25, 2019 at 4:43 pm · Reply
Will the memory allocated to a vtable of a class, will appear when we do sizeof(class)??
https://www.learncpp.com/cpp-tutorial/125-the-virtual-table/ 9/15
9/17/2019 12.5 — The virtual table | Learn C++
nascardriver
February 26, 2019 at 7:17 am · Reply
The pointer to the vtable is stored in the class, this will have an effect (Usually 8 bytes). The
vtable entries don't affect the class' size.
Ganesh Koli
August 15, 2019 at 11:37 pm · Reply
Hunter
December 18, 2018 at 12:21 pm · Reply
I'm a little confused about how virtual functions work. How does a Base pointer pointing to Derived object know it is
pointing at a Derived object in order to resolve the virtual function call? If I understand correctly, the vtable is supposed
to help with this, but it's still a little fuzzy to me.
Thanks,
Hunter
Alex
December 18, 2018 at 1:19 pm · Reply
The virtual table for each class points to the most-derived versions of functions available to
objects of that class. The virtual table pointer in a class object points at the virtual table for the
Derived class.
Therefore, for a Base pointer pointing to a Derived class, baseptr->__vptr will be pointing to Derived's virtual
table (because the object is actually a Derived object, and therefore __vptr will be set to point to Derived's
virtual table, per the above), and thus any function call made through that table will route to the most derived
version of the function that a Derived object can call.
Hunter
December 18, 2018 at 1:49 pm · Reply
Thank you!
Hunter
Digendra Chand
September 28, 2018 at 3:58 am · Reply
class Base
{
public:
virtual void function1() {}
https://www.learncpp.com/cpp-tutorial/125-the-virtual-table/ 10/15
9/17/2019 12.5 — The virtual table | Learn C++
As we can see in derived class D1 we have not overrided function2(). So as we know the V_table of class D1 will have
only function1() in it.
Now
int main ()
{
D1 d;
D1*p =&d;
p->function2();
return 0;
}
nascardriver
September 28, 2018 at 4:06 am · Reply
If you're not overriding a virtual function you're inheriting the parent function
sam
September 23, 2018 at 6:40 am · Reply
I think "But what if Base really pointed to a Base object..." should be "But what if dPtr really pointed to a
Base object...".
Having great fun with these lessons by the way and I'm glad to see you're updating the site too.
Alex
September 24, 2018 at 7:59 am · Reply
Vijay
July 3, 2018 at 4:41 am · Reply
Someone please clarify, if there is an overridden virtual function in derived class then does the derived class VTABLE
will contain the addresses of non-overridden functions of base class as well?
Please consider the folowing case:
class base
{
public:
int x;
virtual void f0() {}
https://www.learncpp.com/cpp-tutorial/125-the-virtual-table/ 11/15
9/17/2019 12.5 — The virtual table | Learn C++
};
};
Please clarify: will the derived class VTABLE will also contain the addresses of base class's virtual functions:
base::f0
base::f1
f0 (derived class overridden f0)
f1 (derived class overridden f1)
OR
the derived class's VTABLE will only contain the addresses of overridden derived class's functions:
f0 (derived class overridden f0)
f1 (derived class overridden f1)
As per the explanation provided in the below discussion, it seems like the derived class's VTABLE will only contain the
addresses of overridden derived class's functions i.e. f0 and f1 (overridden in derived class). However, a clarification
from someone would be very helpful to avoid ant confusion.
Regards
nascardriver
July 4, 2018 at 7:30 am · Reply
Hi Vijay!
The VTable of @derived1 will only contain the overridden functions, not the original ones.
Vijay
July 5, 2018 at 1:02 am · Reply
Mehul Shah
June 5, 2018 at 4:58 am · Reply
Hi Alex,
1 class Base
2 {
3 virtual void test()
4 {
5 }
6 }
https://www.learncpp.com/cpp-tutorial/125-the-virtual-table/ 12/15
9/17/2019 12.5 — The virtual table | Learn C++
7
8 class Derived : public Base
9 {
10 virtual void test()
11 {
12
13 }
14 }
15
16 int main()
17 {
18 Base obj1;
19 Base obj2;
20 }
1) As far as my understanding is there, vptr is a intance variable not a class variable, is it corrent ?
2) Base and Derived will have only 1 vptr or base has its own vptr and derived has its own vptr
3) in the above code when i run in visual studio 2015 i see the address of obj1.vptr and obj2.vptr is same. how is this
possible as vptr is a intance variable not class variable
Regards,
Mehul
nascardriver
June 5, 2018 at 5:14 am · Reply
Hi Mehul!
1) Correct.
2) The classes don't have vtable pointers, they have vtables. @Base has it's own vtable and @Derived has it's
own vtable.
3) Each instance has it's own vtable pointer, this can point to whichever vtable you like. In your example you
have two vtables, one for @Base and one for @Derived. You also have two vtable pointers, one in @obj1 and
one in @obj2. Since both objects are of the same type (Base) they'll both point to the vtable of @Base.
Dimon
May 12, 2018 at 4:40 pm · Reply
moreover, the order of functions placed in the VFTs of each derived class is exact as they declared in
the base object. that's all magic. let me show you:
1 class base
2 {
3 public:
4 int x;
5
6 virtual void f0() {}
7 virtual void f1() {}
8 virtual void f2() {}
9 virtual void f3() {}
10
11 };
12
13 class derived1 : public base
14 {
15 public:
16 int y;
17
18 void f3() override {} // order shuffled
https://www.learncpp.com/cpp-tutorial/125-the-virtual-table/ 13/15
9/17/2019 12.5 — The virtual table | Learn C++
19 void f2() override {}
20 void f0() override {}
21 void f1() override {}
22 };
23
24 class derived2 : public base
25 {
26 public:
27 int y;
28 // i left unimplemented f0 and f3
29 virtual void f6() {} // new VF
30 void f2() override {}
31 virtual void f5() {} // new VF
32 void f1() override {}
33
34 };
35
36 void main()
37 {
38 {
39 derived1 der1;
40 derived2 der2;
41 base bas;
42 __debugbreak();
43 }
44 }
0:000> dps poi(der1) L4 (dump VFT of der1, funcs was shuffled, do you remember? )
0022abb8 002212d5 tests!ILT+720(?f0derived1UAEXXZ) f0 at index 0 as in base order
0022abbc 002210aa tests!ILT+165(?f1derived1UAEXXZ) f1 at index 1 as in base order
0022abc0 00221208 tests!ILT+515(?f2derived1UAEXXZ) f2 at index 2 as in base order
0022abc4 00221096 tests!ILT+145(?f3derived1UAEXXZ) f3 at index 3 as in base order
thus the indexes of functions will always be kept, and with each new virtual function table will grow.
and if the some virtual function is unimplemented in derived class, its place in class VF table will be replaced with
pointer to base class VF.
https://www.learncpp.com/cpp-tutorial/125-the-virtual-table/ 14/15
9/17/2019 12.5 — The virtual table | Learn C++
neil
April 30, 2018 at 4:49 pm · Reply
Alex
May 6, 2018 at 8:46 pm · Reply
Mireska
April 24, 2018 at 1:55 am · Reply
I'm so glad that the compiler is very smart and is able to do all this stuff, truly mindblowing
« Older Comments 1 … 3 4 5
https://www.learncpp.com/cpp-tutorial/125-the-virtual-table/ 15/15