0
class A {
public:
    virtual void x() = 0;
    virtual void y() = 0;
    virtual int z() = 0;
};

class B: public A {
public:
    void x();
    void y();
    int z();
    void w();
};

class C: public A {
public:
    void x();
    void y();
    int z();
};

"Class 'i' has virtual method 'j' but non-virtual destructor", where i = A, B C, and j = x, y, z

Do I need a virtual destructor?

Hello
  • 219
  • 2
  • 7
  • If you need virtual methods you need a virtual destructor. Use case is the same both times. – user207421 Oct 07 '17 at 05:17
  • 2
    @EJP *Need* might be a strong word. There's clearly nothing that stops you from having a non-virtual destructor. You only really *need* it if you want to `delete` via a pointer the base class. It's definitely good practice to make the destructor virtual if you have virtual functions, though (or you could make the destructor protected). – Justin Oct 07 '17 at 05:36
  • There is no strong need to make the destructor virtual any more. Custom deleters of standard smart pointers do the same thing. There is however almost never a reason not to make the dtor virtual, as it comes for nearly free with any virtual function. – n. m. could be an AI Oct 07 '17 at 07:18

0 Answers0