[
Note 1:
However, when the
cast-expression
of a
delete-expression
refers to an object of class type,
because the deallocation function actually called is looked up in the scope of
the class that is the dynamic type of the object
if the destructor is virtual, 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]