[
Note 1:
However, when the
cast-expression
of a
delete-expression
refers to an object of class type with a virtual destructor,
because the deallocation function is chosen by the destructor
of the dynamic type of the object, the effect is the same in that case
. For example,
struct B {
virtual ~B();
void operator delete(void*, std::size_t);
};
struct D : B {
void operator delete(void*);
};
struct E : B {
void log_deletion();
void operator delete(E *p, std::destroying_delete_t) {
p->log_deletion();
p->~E();
::operator delete(p);
}
};
void f() {
B* bp = new D;
delete bp;
bp = new E;
delete bp;
}
Here, storage for the object of class
D
is deallocated by
D::operator delete(),
and
the object of class
E is destroyed
and its storage is deallocated
by
E::operator delete(),
due to the virtual destructor
. —
end note]