简体   繁体   中英

Do I have to delete each (pointer) element of a QSet manually?

I have a class member:

QSet<QDialog*>* dialogs_;  

Do I need to delete just dialogs_ or do I have to call delete on each element of it as well?

This will do the trick:

qDeleteAll(*dialogs_);
delete dialogs_

You can also do it without dereference:

qDeleteAll(dialogs_->begin(), dialogs->end());
delete dialogs_

Yes, you need to somehow manually delete each QDialog in dialogs_ , if it has any.

You can iterate through the QSet and delete them manually yourself. Because QDialog inherits from QWidget , another way is to simply delete the parent of all the dialogs if the parent is allocated on the free store as well, which will in turn delete them.

Note that there's no reason to allocate QSet on the free store, if that's what you're doing. You can save a new / delete operation by simply making it a direct member of your class.

QSet<QDialog*> dialogs_;

That's one less thing you have to worry about wrt manual deletion.

First iterate through your set, delete every object in it, and then delete the set object.

However, take a note that Qt has it's own memory management, and it might be ok just to delete the set, and leave the objects in it to be destructed by the Qt's mechanism.

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