I'm taking a data structure and algorithm in C++ class where we are expected to know about and be able to use vectors, a topic which my previous C++ class neglected to mention. From what little I know about vectors they will make my upcomming assignment much easier so I am going to give them a try. My problem is this: I have a class in which I was planning on putting a vector of structs, but I decided that it would probably more efficient to make it a vector of classes instead. Am I correct in assuming that when the vector is destroyed it will automatically call the destructors for each class it contains, or do I have to code this myself? Also, since I'm using a vector in my first class with no other dynamically allocated space in that class, do I even need a destructor for it? Here's an example of what I'm talking about:
One final question, would it be better if vector<Class2> was vector<*Class2>, and if so how would affect how everything is destroyed? I assume if I make this change Class1 would then need a destructor of its own.Code:class Class1 { // Public Members <- Do I need a destructor here? private: vector<Class2> theVector; // When this is destroyed will it destroy all of the Class2's it contains? // More private members }; class Class2 { // Public Members <- I know I need a destructor here. private: char *aString; // Space for this will be dynamically allocated. // More private members };
Thanks in advance.