Hello again! In this video, we are going to look at how virtual member functions are implemented.
You do not actually need to understand this to be a good C++ programmer.
And you can probably get by without even knowing about it, but it is always useful to have some idea
of what is going on.
So first, let's remind ourselves how member functions are implemented. Member functions are not stored
in the object, they are implemented as global functions.
The only difference is that, when we call a member function, the compiler will add an extra argument
with the "this" pointer, which is a pointer to the object that the member function was called on.
So if we have a class which has some non-virtual member functions, it's really not that different from
having a global function.
There is quite a big difference, though, for virtual member functions.
First of all, when the compiler sees that a class has a virtual member function, it will create and
populate a data structure for the class, which is the virtual member function table, or vtable for
short.
And this has information about the class's virtual member functions. When the compiler sees that we are
calling a virtual member function.
It will generate some extra code, which is executed at run-time.
This will check the dynamic type of the object, and then it will use the vtable for that dynamic type,
to call the correct version of the function.
So if we have a class which has virtual member functions, then the calls will go through this intermediate
vtable.
This vtable has the address of all the virtual member functions of the class. The vtable is actually
an array of pointers to the class's virtual member functions, and each virtual member function of the
class is identified by its index into this array.
When a virtual member function is called, the compiler will replace the name of the function. Not
with a pointer like it would for a normal member function,
but with the index into the vtable. And then the run-time code,
once it has found out which vtable to use, it will look up the element in the vtable,
which has that index, and then it will call the member function through that function pointer.
So this is all very clever, but there is a price to be paid. When we call a virtual member function,
we have to go through this vtable, which adds indirection.
So, typically, calling it takes about 25 or 50 percent longer than calling a non-virtual member function.
There is also some memory usage, each class that has a virtual member function will need a vtable, so
that is an array in memory at run-time, and each virtual member function in the class will require a
pointer in this array.
So if you have a very large hierarchy, with lots of classes with virtual member functions, or if you
call virtual functions in a very tight loop, you may find that it affects the performance of your
program.
So the advice is, only use virtual member functions when you really need the extra flexibility.
Okay, so that is it for this video.
I will see you next time, but until then, keep coding!