I am trying to delete the following map:
typedef bool (myClass::*func)(std::vector<std::string> &args); typedef std::map<std::string, func> myMap; myMap map; //map of functions in the class 'myClass' void deleteMap() { for(myMap::iterator it = map.begin(); it != map.end(); ++it) { delete it->second; //compiler says it->second is non-pointer type map.erase(it); } } 'map' is mapping a string to a function in the class 'myClass', and this function takes a vector of strings in its parameter.
In my attempt to delete this map I am trying to delete the pointer to the member function, then erase the iterator itself. The compiler says that it->second must be a pointer type. In the typdef 'func' is a pointer to myClass:: so why am I getting this error?
Is this the appropriate way to delete a map of functions?
myClass. Just take a short moment to think about the implications of this!deleteonly works for memory that has been dynamically allocated on the heap, (member) functions are all in the static read-only part of memory. Simply never try to delete a function pointer. Emptying the map usingeraseorclearshould be enough