defining pure virtual destructor?

Started by
5 comments, last by Sneftel 14 years, 5 months ago
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



I trust exceptions about as far as I can throw them.
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.
But they already declared doit to be pure virtual.
I trust exceptions about as far as I can throw them.
Just typical C++ mess.
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.
-Mike
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.
// Full Sail graduate with a passion for games// This post in no way indicates my being awake when writing it
The standard says it's fine.

This topic is closed to new replies.

Advertisement