0% found this document useful (0 votes)
2 views9 pages

Lecture - Dynamic Polymorphism - Part 2

In C++, a virtual table (vtable) is a compiler-generated table of function pointers that supports runtime polymorphism by allowing the correct method implementation to be chosen based on the object's actual type. Each class with virtual functions has a hidden virtual pointer (vptr) that points to its vtable, enabling dynamic method dispatch. Key concepts include virtual functions, vtables, and the importance of virtual destructors for proper cleanup in inheritance hierarchies.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Lecture - Dynamic Polymorphism - Part 2

In C++, a virtual table (vtable) is a compiler-generated table of function pointers that supports runtime polymorphism by allowing the correct method implementation to be chosen based on the object's actual type. Each class with virtual functions has a hidden virtual pointer (vptr) that points to its vtable, enabling dynamic method dispatch. Key concepts include virtual functions, vtables, and the importance of virtual destructors for proper cleanup in inheritance hierarchies.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

virtual table

What is vTable?

In C++, a virtual table (commonly called a vtable) is a mechanism used to support runtime
polymorphism. It's used when you declare a method as virtual in a base class, allowing the
correct method implementation to be chosen at runtime, depending on the actual type of the
object.

The vTable, or Virtual Table, is a table of function pointers that is created by the compiler to
support dynamic polymorphism. Whenever a class contains a virtual function, the compiler
creates a Vtable for that class. Each object of the class is then provided with a hidden pointer to
this table, known as Vptr.
What is vPtr (Virtual Pointer)?

The virtual pointer or _vptr is a hidden pointer that is added by the compiler as a member of the
class to point to the VTable of that class. For every object of a class containing virtual functions,
a vptr is added to point to the vTable of that class. It's important to note that vptr is created only
if a class has or inherits a virtual function.
Key Concepts
Virtual Function: A function in a base class declared with the virtual keyword. It can be
overridden in a derived class.
Vtable: A hidden table maintained by the compiler for each class with virtual functions. It stores
pointers to the virtual functions that can be called on instances of that class.
Vptr (Virtual oinPter): Each object of a class with virtual functions contains a hidden pointer
(vptr) that points to the class's vtable.
#include <iostream>
using namespace std;

class Base {
public:
virtual void show() {
cout << "Base class show function\n";
}
};

class Derived : public Base {


public:
void show() override {
cout << "Derived class show function\n";
}
};

int main() {
Base* b; // Base class pointer
Derived d; // Derived class object
b = &d;
b->show(); // Calls Derived::show() due to vtable
return 0;
}
How It Works Behind the Scenes
When Derived d; is created, it has a vptr pointing to the Derived's vtable.
When you call b->show();, the vptr is used to look up the show() function in the vtable.
Because b points to a Derived object, the Derived class’s version of show() is called.
class A {
int x;
public:
virtual void foo() { std::cout << "base::foo()\n"; }
virtual void bar() = 0;
virtual ~A() {}
};

class B : public A {
int y;
public:
virtual void bar() { std::cout << "Derived::bar()"; }
virtual void baz() { std::cout << "Added function"; }
};

int main() {
A a;
B b;
}
Summary
Only classes with virtual functions have a vtable.
Vtables are compiler-specific (implementation details may vary).
Virtual functions introduce a slight overhead due to the indirection.
Use virtual destructors in base classes to ensure proper cleanup when deleting derived objects
through base pointers.

You might also like