I have some specific questions on virtual destructors and vtable.
Suppose I have the following code:
class Base
{
public:
virtual ~Base();
};
class Child : public Base
{
public:
~Child();
};
Questions:
The explanation below assumes that virtual dispatch implementation used by the compiler is based on virtual tables.
Each class with virtual methods (declared or inherited) has its own virtual table. If a subclass overrides a virtual member function in the base, a pointer to the overriding function is placed in the class's vtable; otherwise, a pointer to base class implementation is kept in place.
Adding the first virtual function increases the size of the class instance by the size of vtable pointer. Virtual functions after the first one do not add to the size of the instance.
Since ~Base
is virtual, the ~Child
is virtual as well, even though virtual
keyword is omitted. In case of an override, virtual
keyword is optional.