How to access the vtable of an abstract class


It’s hard because abstract class has no instance. When you create an instance, the vtable belongs to the it’s sub class.

The question is:
There is an abstract class (with pure virtual functions) that’s marked with novtable, how do you access it’s vtable (and therefore, a violation follows since there is no vtable)?
Below is the only way I’ve found.

struct __declspec(novtable) CBase
{
 CBase()
 {
  ((CBase*)this)->f();
 };
 virtual void f() = 0;
};
struct CSub: public CBase
{
 void f()  {  }
};
void TestVTable()
{
 CSub();
}

Two keys here:
1. f() must be called inside base constructor
2. ‘this’ must be casted.

Advertisements
Comments are closed.
%d bloggers like this: