Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a pointer from QMap?

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);
like image 687
elgolondrino Avatar asked Dec 11 '22 10:12

elgolondrino


1 Answers

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;
like image 144
vahancho Avatar answered Dec 28 '22 19:12

vahancho