65.9K
CodeProject is changing. Read more.
Home

Displaying vtable when debugging

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.89/5 (9 votes)

Jul 1, 2010

CPOL
viewsIcon

35597

This tip show how to display all entries from a C++ vtable in the VS debugger.

Due to some limitations the virtual table of an object is not shown by default in the debugger. In some cases only the entries from the base class is shown.
class Base
{
    virtual void a();
};
class Derived : public Base
{
    virtual void b();
};
Base* ptr = new Derived();
which in a watch window gives
ptr->__vfptr	0x004420f8 const Derived::`vftable'
	[0x0]	0x00415ed3 Base::a(void)
You can look at the entire vtable if you add this helper variable:
void (**vt)() = *(void (***)())ptr;
and then inspect vt,X in a watch window, where X is the number of expected virtual function entries. vt,2 gives in the example above
vt,2	0x004420f8 const Derived::`vftable'
  [0x0]	0x00415ed3 Base::a(void)
  [0x1]	0x00415ee2 Derived::b(void)
Idea originates from here[^], and it's too good to not share.