How do I create a generic std::vector destructor?
c++, destructor, stl, vector
Solution
The best thing to do is use smart pointers, such as from Boost. Then the objects will be deleted automatically.
Or you can make a template function
template <class T>
void vectorCleanup(vector<T *>& dVector){
T* tmpClass;
for(vector<T*>::size_type i = 0; i < dVector.size(); i++){
tmpClass = dVector[i];
delete tmpClass;
}
}
Problem
Having a vector containing pointers to objects then using the clear function doesn't call the destructors for the objects in the vector. I made a function to do this manually but I don't know how to make this a generic function for any kind of objects that might be in the vector. ``` void buttonVectorCleanup(vector<Button *> dVector){ Button* tmpClass; for(int i = 0; i < (int)dVector.size(); i++){ tmpClass = dVector[i]; delete tmpClass; } } ``` This is the function I have that works fine for a specific type of object in the vector but I'd like a single function that could take any kind of vector with object pointers.