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

André Pönitz apoenitz at t-online.de
Thu May 5 13:24:44 CEST 2016


On Thu, May 05, 2016 at 04:24:48AM +0300, Nikos Chantziaras wrote:
> On 04/05/16 21:35, Thiago Macieira wrote:
> >On quarta-feira, 4 de maio de 2016 20:56:52 PDT Nikos Chantziaras wrote:
> >>  From what I can tell, QPointer does not delete the object, so that's a
> >>no go.
> >>
> >>Putting the objects on the stack doesn't work either, because the Qt
> >>object model does not support that (it will call 'delete' on the object
> >>even if it's on the stack.)
> >
> >So what are you requirements? Do you want to a container that does delete the
> >object it has, unless the parent has already deleted it?
> 
> Well, basically what I want is not having even a single 'new' or 'delete'
> anywhere in my code. I already achieved that with all my non-Qt projects.

remedyforirrationalfear.h:

 namespace snakeoil {
 template<typename T, typename... Args>
 inline T* make_dynamic_object(Args&&... args)
    { return new T(std::forward<A>(args)...); }
 }

myposhapp.cpp:

 #include "remedyforirrationalfear.h"

 // Oh Noes! Get out the garlic!
 // auto x = new X(23);
 auto x = snakeoil::make_dynamic_object<X>(23);

> By now, I consider application-level code that uses 'new' or 'delete' as
> bogus and in need of fixing.

What a happy world we live in.

Andre'



More information about the Interest mailing list