I've seen many sites talking about the correct way to implement a d'tor for a class that holds a map. But not for the case where the values of the map themselves are dynamically allocated.
For example, let Manager
be a class which hold map<int, User*>
where User
is some class which I'll allocate dynamically later.
By the rules of the exercise, it should handle a registerUser(string name)
function, which creates a new User
instance and adds it to the map.
Something like:
User* registerUser(std::string userName) {
User* pNewUser = new User(userName);
// Setting some stuff
auto ret = users.insert(std::pair<int, User*>(pNewUser->id, pNewUser));
// Finishing and returning a pointer to the new allocated User
}
AND TO THE QUESTION ITSELF:
Should the d'tor do something special beyond users.clear()
?
Will the memory be freed successfully or shall I iterate over the elements and delete them?
Thank you in advance :)