Curtis L. Olson wrote:
> For the C++ experts, I'm starting to see a lot of these sorts of
> warnings? Other than trying to avoid and clean up warning litter, is
> this dangerous?  What's the best fix for this?
>
> warning: `class FGGlobals' has virtual functions but non-virtual destructor

If you have virtual functions, then the assumption has to be that you
will be using the class polymorphically.  If you are using it
polymorphically, you might be expected to delete objects using a pointer to
the base class instead of the "real" class.  If you do this with a
non-virtual destructor, bad things ensue. :)

More tersely, if you have:

 class SubClass : public ParentClass { ... };

 ParentClass * p = new SubClass(...);
 delete p;

If ParentClass has a *non* virtual destructor, then this code will
call only ParentClass's destructor.  The SubClass destructor will
never be invoked on this SubClass object, and you will leak memory, or
resources, or leave dangling pointers, or worse.

A virtual destructor works by first calling the SubClass destructor,
and then automatically chaining in calls to the superclass destructors
in order to make sure that everything is cleaned up.

So basically, put a "virtual" in front of the destructor declaration
for any classes whose subclasses can be deleted through pointers to
the base class.  It's easy and safe and the Right Thing.  The compiler
warning doesn't necessarily indicate a problem; but there really
aren't any downsides do heeding it anyway.

Andy

-- 
Andrew J. Ross                NextBus Information Systems
Senior Software Engineer      Emeryville, CA
[EMAIL PROTECTED]              http://www.nextbus.com
"Men go crazy in conflagrations.  They only get better one by one."
 - Sting (misquoted)


_______________________________________________
Flightgear-devel mailing list
[EMAIL PROTECTED]
http://mail.flightgear.org/mailman/listinfo/flightgear-devel

Reply via email to