I'm building a hashmap class that can have string keys and ints, bools, strings or pointers of different types as its values, and I want it to work. For the program I'm using it for I create the pointer and pass it into the hashmap. The problem comes when I need to destruct the map. If the type for the hashmap is a pointer I need to delete it(the value) before I delete it's container.
so the code I have right now goes something like this: I have a hashNode** drawers, which I use as a two dimensional array to hold pointer to hashNodes in the map. Those same pointers are also held in another hashNode** array, which stores them as they are added to map (for ease/speed of growing and copying the hashmap).
template <typename V>
class str_map {
public:
// ...
virtual ~str_map() {
str_map<V>::~str_map();
}
// ....
};
and then later I have a bunch of methods like these: one for regular values:
template <>
str_map<int>::~str_map() {
for(int i=0; i < count && array[i] != NULL; i++){
delete array[i];
}
delete array;
delete drawers;
}
and one for pointers:
template <>
str_map<str_map<int>*>::~str_map() {
for(int i=0; i < count && array[i]->val() != NULL; i++)
delete array[i]->val();
for(int i=0; i < count && array[i] != NULL; i++){
delete array[i];
}
delete array;
delete drawers;
}
Is there another better way to deconstruct an instance of str_map class correctly so that all the memory is handled correctly? Or at least a way to make this work?