dimanche 7 juillet 2019

Why virtual destructor required operator delete?

Consider the following code:

class Base {
public:
#ifdef __VIRTUAL__
   virtual ~Base() {}
#else
   ~Base() {}
#endif
};

class Derived : public Base {
public:
    ~Derived() {}
private:
    static void operator delete(void*) = delete;
};

int main() {
    Derived d;
}

It'll compiled successfully with cmd

g++ -std=c++11 main.cpp

but failed with cmd

g++ -std=c++11 -D__VIRTUAL__ main.cpp

It means that if I use virtual destructor function, I cannnot delete operator delete .

Why is this happened, why virtual destructor required global operator delete even if created on stack.

Aucun commentaire:

Enregistrer un commentaire