Search code examples
c++virtualvtable

Is there any relation between Virtual destructor and Vtable


If we write virtual function it adds a vtable in object of that class. Is it true for virtual destructor too ? Is vtable used to implement virtualness of destructor


Solution

  • Yes. Some information is needed to allow the right destructor to be called when the object is deleted via a base class pointer. Whether that information is a small integer index or a pointer doesn't matter (although dynamic linkage probably implies that it's a pointer). Naturally, that information needs to be adjacent to (inside) the pointed-to object.

    Adding a virtual method of any kind, including a destructor, to a class that had none before, will increase sizeof(class).