Musen Musen - 1 year ago 78
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 {
public:
vector<int> intVector;
A() {
intVector.push_back(1);
};
~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?

Answer Source

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)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download