mvce:
#include <memory>
#include <iostream>
using namespace std;
struct _A {
~_A() {
cout << "~_A()" << endl;
}
};
struct A : public _A {
~A() {
cout << "~A()" << endl;
}
};
int main(int argc, char** argv) {
{
shared_ptr<_A> p = make_shared<A>();
} //~A() called
return 0;
}
output:
~A()
~_A()
Here the destructors are not virtual but somehow the shared_ptr
'knows' it points to an A
. Is this reliable or is this undefined behavior ? I want to know if it is necessary I flag the destructors with virtual in this case - assuming I don't work with raw ptrs.