Suppose a class contains pointers and map of strings. In the destructor, apart from deleting the pointers, should we also clear the map or would this be automatically done?
If you have a member variable like std::map<>
(that you've stack-allocated) then you can rely on its destructor when your class destructs. The standard mandates that std::map
will release all its memory once it's destroyed.
If you declare it as a pointer and you have used new
, then you ought to use delete
in your destructor. Just be careful not to have any exceptions thrown in your destructor as that will cause memory faults.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.