[Development] parented_ptr

Daniel Schürmann daschuer at mixxx.org
Thu Oct 16 13:46:36 CEST 2025


Hi André

Nice that you can confirm that there is a demand from others for a
solution.

> Perhaps it might be easier or better to add a make_child template method
to QObject that returns a new instance of the template type, parented to
the object you call it on?

That's actually the solution I was looking for.
I love that idea. Thank you.

What type is the returned pointer? I like to have one that has the
semantical guarantee of being a child object. This is beneficial to
distinguish classic owned pointer member variables from borrowed from the
Qt Object tree.

Do you have an idea?

While the code is kind of trivial, the main benefit it the semantically
effect that you are able to annotate a pointer as patented in a
standardized syntax across code base. You don't have to learn custom
solution and a upstream provided version will help to make this approach a
recommendation standard style.

> I don't quite see how you expect this to find a place in the current API

The moc generated code may use it for instance.

Getter that return an patented object pointer to another object are
borrowed pointers and doing it via a raw pointer is just in the sense of
the C++ Core Guidelines.

We need to consider the case where this pointer will be stored, creating a
shared ownership. For this case the QPointer is designed, but it does not
have the "patented" semantic.  Maybe we need another pointer for that?


Cheers,

Daniel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20251016/e2590445/attachment.htm>


More information about the Development mailing list