[Interest] What's the recommended way to put QObjects in smart pointers?

André Somers andre at familiesomers.nl
Wed May 4 20:11:33 CEST 2016



Op 04/05/2016 om 16:48 schreef Nikos Chantziaras:
> I've been removing every trance of 'new' and 'delete' from my code and 
> switching to something like:
>
>   auto obj = std::make_unique<Type>(ctor args);
>
> (Or std::make_shared, depending on the situation.)
>
> But for QObjects with a parent, that can go horribly wrong. So I have 
> to do annoying stuff like:
>
>   auto dialog = new QDialog(parent);
>   // work...
>   delete dialog;
>
Assuming the above is one function, you could just do:

auto dialog = QDialog(parent);
//work

//end of scope, dialog gets deleted

André




More information about the Interest mailing list