简体   繁体   中英

Delete QMap value as a pointer

I have a QMap with key QString and with the value myClass . I've looked through the Qt documentation and decided to define value as pointer to myClass object. But, I'm puzzled with issue of how to remove both key and (pointer object allocated by new operator) value? In case, if I used remove() method, would that be safe in terms of memory release of dynamically allocated object?

Is this a good alternative?

myClass *type = types.take("foo");
delete type;

As Qt documentation says about QMap::take() function

Removes the item with the key key from the map and returns the value associated with it.

So that your code will remove the key from the map and delete the allocated memory for myClass object.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM