I have a class hierarchy that's three levels deep, like this:
class A {
public:
virtual ~A() {}
}
class B : public A {
public:
virtual ~B() {}
void foo(E *e) {
e->remove(this);
}
}
class C : public B {
public:
~C() {}
}
class E {
public:
void remove(A *a) {
delete a;
}
}
Ok so what I am wondering is what happens when I call foo()
on an object of C
. Is it going to remove the entire object or only the B
and A
part of the object, and leave the C
part still in memory?
Is it going to remove the entire object or only the B and A part of the object, and leave the C part still in memory?
No. It will "do the right thing" (that is, delete the most derived subobject, run all its destructors etc.) provided A
(that is, the static type of the pointee of the pointer you delete
) has a virtual destructor (and if class A
has a virtual destructor, all its descendants have it, too). This holds for multiple inheritance, too.