Question

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;
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top