문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top