I want to inherit from std::map, but as far as I know std::map hasn’t any virtual destructor.
Is it therefore possible to call std::map‘s destructor explicitly in my destructor to ensure proper object destruction?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The destructor does get called, even if it’s not virtual, but that’s not the issue.
You get undefined behavior if you attempt to delete an object of your type through a pointer to a
std::map.Use composition instead of inheritance,
stdcontainers are not meant to be inherited, and you shouldn’t.I’m assuming you want to extend the functionality of
std::map(say you want to find the minimum value), in which case you have two far better, and legal, options:1) As suggested, you can use composition instead:
2) Free functions: