I've got a QMap with the QString key and with value pointer to an Object of myclass. But I don't know how to delete a pointer from QMap when I allocate the value of QMap dynamically:
QMap<QString, myClass*> types;
myClass *type = types.value(typeKey);
if (!type) {
type = new myClass;
types.insert(typeKey, type);
How shall I delete a pointer by a key?
I'm aware of QMap methods like remove
. Is that safe to use?
What about the following:
const QString key = types.key(static_cast<myClass*>());
types.remove(key);
The remove()
function removes the item from the map, however it does not delete it, so you have to do it yourself, if it is a pointer to the object. I would do that in the following way:
myClass *type = types.take("foo");
delete type;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With