1

Are some difference between virtual destructor and pure virtual destructor? In my design I always used pure virtual destructor:

class MyInterface {
public:
    virtual ~MyInterface() = 0;
    virtual void doA() const = 0;
    virtual void doB( int ) = 0;
};
inline MyInterface::~MyInterface {}

Well I know why the dctor must be virtual but I don't understand the difference with this:

class MyInterface {
public:
    virtual ~MyInterface() {}
    virtual void doA() const = 0;
    virtual void doB( int ) = 0;
};
Elvis Dukaj
  • 7,142
  • 12
  • 43
  • 85
  • Pure virtual means the class can not be instantiated. Any derived class must override the pure virtuals with non-pures or the derived class can not be instantiated also. – brian beuning May 23 '13 at 21:54
  • Is the first one really a pure virtual destructor, since you are providing an inline implementation? – jxh May 23 '13 at 21:56
  • I've to implement it, this is an exception to the rule to allow the call of base dctor from derived – Elvis Dukaj May 23 '13 at 21:56
  • @brianbeuning: AFAIK, there is no way to override a pure virtual destructor. So it leads me to question its existence. We need a virtual destructor, for sure, but "pure virtual destructor" seems like an oxymoron. – jxh May 23 '13 at 21:59
  • I've read the post that Andy has suggested... for what I've understand is just to force derived class to clean-up, nothing more – Elvis Dukaj May 23 '13 at 22:03
  • [Pure virtual destructor in C++](http://stackoverflow.com/questions/630950/pure-virtual-destructor-in-c) is more relevant. The difference is that the first one should require the derived class to implement a destructor (but I can't find a compiler that enforces that, so they are equivalent). – jxh May 23 '13 at 22:04
  • A pure virtual destructor can also be used to make a class abstract, even without any other pure virtual methods. – Andy Thomas May 23 '13 at 22:05
  • the difference is that first snippet require that derived class implements dctor, 2nd snipped instead doesn't – Elvis Dukaj May 23 '13 at 22:09
  • @user315052 You cannot find a compiler that "enforces" that because the rules say that if you don't provide a destructor the compiler creates one for you. So the derived class **always** implements the destructor, and therefore all is well. – Nik Bougalis May 23 '13 at 22:38

0 Answers0