I have an class A
which uses a heap memory allocation for one of its fields. Class A is instantiated and stored as a pointer field in another class (class B
.
When I'm done with an object of class B, I call delete
, which I assume calls the destructor... But does this call the destructor of class A as well?
Edit:
From the answers, I take that (please edit if incorrect):
delete
of an instance of B calls B::~B();- which calls
A::~A();
A::~A
should explicitlydelete
all heap-allocated member variables of the A object;- Finally the memory block storing said instance of class B is returned to the heap - when new was used, it first allocated a block of memory on heap, then invoked constructors to initialize it, now after all destructors have been invoked to finalize the object the block where the object resided is returned to the heap.