C++: Subclass destructors... is the virtual inherited?

This topic is 3631 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Quick question. So I have a base class, and I make the destructor virtual to make sure any inherited classes are cleaned up properly:
class Base
{
virtual ~Base() {}
}

Now, if I make a child class that inherits from Base, and it doesn't need to deallocate anything for itself, do I need to explicitly make another empty virtual destructor so that any further subclasses get cleaned up, or does the virtual carry through from the Base. Example:
class Sub : public Base
{
// Various methods, but nothing to clean up so no destructor
}

class SubSub: public Sub
{
~SubSub();  // This has a destructor!!  Now if I call delete on a Base*,
// will this destructor still get called without Sub also
// having a virtual destructor?
}

I know I could just be safe and put a virtual destructor in each class, but I like to have minimal code, I find it makes everything easier.

Share on other sites
Yes, virtual will implied for the version in derived classes. This applies to all functions declared virtual in the base class.

Share on other sites
Thanks.

To make sure I have a grasp on this: The virtual is implied for all functions in derived classes if these derived classes don't override the functions. Is this true also if the function is overridden? Or do I need to mark the overridden version virtual?

(In other words, from my previous example, should SubSub's destructor be virtual to "continue the virtuality"?)

Share on other sites
See C++ FAQ Lite entries [11.12] and [20.7]

Share on other sites
Hmm... the faq seems to make it seem as if once a method is declared virtual, it will remain virtual in all overridden versions, even if they're not marked virtual.

This comes as a surprise to me, as I recall reading in a recent book that you have to explicitly mark later versions virtual as well, or the "virtualness" ends there.

Perhaps the book I was reading was incorrect/out of date?

Share on other sites
Quote:
 Hmm... the faq seems to make it seem as if once a method is declared virtual, it will remain virtual in all overridden versions, even if they're not marked virtual.

That's correct

Quote:
 Perhaps the book I was reading was incorrect/out of date?

Very possibly. C++ has existed since the mid 1980s, but was only standardised in 1997. Anything written before then is most liekly useless. It's also a very complicated language - many of the people writing about it on the internet aren't expert and make mistakes. The C++ FAQ Lite is a trustworthy source though.

Share on other sites
Ah, this site explains this very clearly:
IBM Reference
It's the reference for IBM's AIX C++ Compiler, but I assume (perhaps foolishly[wink]) it follows the standard pretty close.

In particular this line covers it (B is subclass of A):
Quote:
 If you declare a function named f in class B with the same name and same parameter list as A::f, then B::f is also virtual (regardless whether or not you declare B::f with the virtual keyword) and it overrides A::f.

Out of curiosity, does anybody recommend a good reference for C++ that complies with the standard? (though the standard will change soon with C++0x) Is Stroustrup's book best for that? I've been meaning to get it regardless but am waiting for the time being as it's a tad pricey. There's also the actual standard which you can download, is that a good idea? I'm concerned it'll be filled with stuff that's mostly of use to people creating/maintaining compilers and not so much for programmers who just want to use the language.