Skip to content
QuantReadySign In
#1805easyC++ & Systems

Deleting a Derived Object Through a Base Pointer

Consider the following C++ program:

#include <iostream>
#include <string>

struct Base {
    ~Base() { std::cout << "~Base\n"; }
};

struct Derived : Base {
    std::string name{"a name comfortably longer than any SSO buffer"};
    ~Derived() { std::cout << "~Derived\n"; }
};

int main() {
    Base* p = new Derived;
    delete p;
}

According to the C++ standard, what is the behavior of this program?

Loading interactive editor…