
Virtual destructor question
Quote:
> Suppose I have some code like the following:
> class Base {};
> class Derived : public Base {};
> Base *p = new Derived;
> delete p;
> I know that for this to work correctly, Base has to have a virtual
> desrtuctor.
Not necessarily. If neither Base nor Derived have any resources to free,
then having any destructor is unnecessary. But, in most cases, you are
correct and Base must have a virtual destructor.
Quote:
> But does Derived have to have a virtual destructor? (Assume
> that no class inherits from Derived).
Derived WILL have a virtual destructor because it is a derived class of a
class having a virtual destructor. If a compiled class can find the
destructor of a Base object via the vtable, it must also be able to find the
destructor of any derived class in the same way.
Do you have to declare this? No. But it eliminates confusion and lookup
time on your maintainers' part to declare it as such anyway.
faa
--