Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Gamer Gamester   Members   -  Reputation: 136

Like
0Likes
Like

Posted 08 February 2008 - 12:28 PM

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.

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9669

Like
0Likes
Like

Posted 08 February 2008 - 12:30 PM

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

#3 Gamer Gamester   Members   -  Reputation: 136

Like
0Likes
Like

Posted 08 February 2008 - 12:37 PM

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"?)

#4 sigsegv42   Members   -  Reputation: 158

Like
0Likes
Like

Posted 08 February 2008 - 12:39 PM

See C++ FAQ Lite entries [11.12] and [20.7]

#5 Gamer Gamester   Members   -  Reputation: 136

Like
0Likes
Like

Posted 08 February 2008 - 12:44 PM

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?

#6 Nitage   Members   -  Reputation: 810

Like
0Likes
Like

Posted 08 February 2008 - 12:50 PM

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.

#7 Gamer Gamester   Members   -  Reputation: 136

Like
0Likes
Like

Posted 08 February 2008 - 04:22 PM

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.

#8 sigsegv42   Members   -  Reputation: 158

Like
0Likes
Like

Posted 08 February 2008 - 05:46 PM

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++.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS