Musen Musen - 5 months ago 28
C++ Question

If I declare a vector in a C++ class, will it be destroyed then the instate of class is destroyed?

Say I have such a class

class A {
vector<int> intVector;
A() {

int main() {
A *k = new A;
delete k;

When A is instantiated as k, intVector will be created. However, my question is, if the destructor does not take care of intVector, will the memory allocated for intVector be recycled when the pointer of k is destroyed?


Yes, the compiler will generate code inside the destructor of the class to destroy all member variables (and call any base-class destructors that a class is derived from)