Rockbox mail archiveSubject: Re: Destructors in Theme Editor: virtual or non virtual?
Re: Destructors in Theme Editor: virtual or non virtual?
From: Robert Bieber <robby_at_bieberphoto.com>
Date: Thu, 17 Jun 2010 14:11:26 -0400
On 06/17/2010 04:27 AM, Al Le wrote:
> Out of curiosity I looked at the code of the Theme Editor which is written in C++. And saw, e.g. in the class SkinViewer, that the destructor is declared as a non-virtual function. But shouldn't (almost always) destructors be declared as virtual functions so that the framework (Qt in this case) can correctly destroy custom objects?
> Just digging out some long forgotten C++ rests out of my memory...
You are correct, I should be making them virtual (I'll go and fix that
shortly). Strictly speaking, it isn't an issue unless you declare
children of a class that has a non-virtual destructor. The compiler
will always ensure that all parent-class destructors are called in order
at destruction, but if you inherit from a class that has a non-virtual
destructor, and then you delete a pointer to the base class which is
also an instance of the child class, the child class' constructor won't
be called. Now that someone else has mentioned it, I do think that
having the original base class constructor virtual makes all the
following destructors virtual, but it's still safest to just explicitly
declare them all that way.
Received on 2010-06-17