Jump to content
  • Advertisement
Sign in to follow this  
Storyyeller

defining pure virtual destructor?

This topic is 3125 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

I noticed in the C++ FAQ Lite section on functors, they declare a pure virtual destructor and define it. Is there any good reason to do this? Why make the destructor pure virtual in the first place? I'm really confused.
class Funct {
 public:
   virtual int doit(int x) = 0;
   virtual ~Funct() = 0;
 };
 
 inline Funct::~Funct() { }  // defined even though it's pure virtual; it's faster this way; trust me



Share this post


Link to post
Share on other sites
Advertisement
Well, without it your program won't link when you derive from that class, so providing it's definition definitely has a reason. But generally you only make the destructor pure virtual if it's the only virtual function in the base class to force it to be an abstract base.

Share this post


Link to post
Share on other sites
I don't trust the comment saying it's faster.

The reason why a pure virtual function might have an implementation is because you want to provide default functionality but force derived classes to make a consious decision about using that functionality. Destructors are kind of special though because you're going to call them whether you want to or not, but it amounts to the same thing - trying to force derived classes to be more careful.

Share this post


Link to post
Share on other sites
I have seen some compilers refuse to compile pure virtual functions that are also defined. I don't know what the standard says about it, but it is generally a bit wonky.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!