We know that if there are virtual functions then the base class destructor should be marked as virtual as well, otherwise it is undefined behavior when explicitly if we hope to delete derived object with base class pointer the base destructor should be marked as virtual, otherwise it is undefined behavior.deleted
with base class pointer
For example,
struct Base {
virtual void greet() { std::cout << "base\n"; }
};
struct Derived : public Base {
virtual void greet() override { std::cout << "derived\n"; }
};
call
Base *b = new Derived;
b->greet();
delete (b);
clang(gcc similarly) will emit such a warning when -Wdelete-non-virtual-dtor:
delete called on 'Base' that has virtual functions but non-virtual destructor
But neither of them report warnings for smart pointers:
std::unique_ptr<Base> sb = std::make_unique<Derived>();
// std::unique_ptr<Base> sb = std::unique_ptr<Derived>(new Derived);
sb->greet();
I guess this still leads to undefined behavior, right?
virtual functions
called, why? – Hongxu Chen