Sign in to follow this  

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Maybe take a look at one of Herb Sutter's Exceptional C++ books. They might be a little cheaper and will definitely be a good source of standards conformant information (Sutter is the chair of the ISO C++ committee). Also, Andrei Alexandrescu's Modern C++.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this