I have a class that has a std::list as a member variable. I also have a vector of these classes. If I delete an element from the vector, will the class destructor be called on the deleted instance? Do I have to explicitly clear my list with list::clear() in the destructor?
class my_class {
string class_name;
list<int> my_list;
public:
my_class();
~my_class(); // destructor
};
vector<my_class> my_vec;
my_class obj1, obj2, obj3;
my_vec.push_back(obj1);
my_vec.push_back(obj2);
my_vec.push_back(obj3);
my_vec.erase(my_vec.begin()+2); //Is this sufficient?
std::vector
follows the RAII principle, which ensures that the object's destructor is called and the object is deleted from memory once the object goes out of scope.
Specifically to your questions:
If I delete an element from the vector, will the class destructor be called on the deleted instance? Yes
Do I have to explicitly clear my list with list::clear() in the destructor? No
Be aware: RAII doesn't do anything with pointers. So, it does not dereference a pointer, nor does it call the destructor on the dereferenced object, nor does it delete the dereferenced object from memory.