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
defining pure virtual destructor?
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.
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.
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.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement