[Development] parented_ptr

Ville Voutilainen ville.voutilainen at gmail.com
Mon Nov 3 16:52:44 CET 2025


On Sun, 2 Nov 2025 at 20:36, Daniel Schürmann <daschuer at mixxx.org> wrote:
> All the discussed ideas of createChild() and addChild() needs in sub
> calls, finally call the correct setParent() overload.

It doesn't need to do that setParent() call, all it needs to do is
call the constructor
with the supplied arguments and add 'this' at the end.

> This depends on both, the parent and the child class type. C++ can
> unfortunately not deduce the type of the "this" pointer in a base class
> template.

Why would it need to deduce it? You have a member template, there's no
need to deduce
anything, the type of the parent is just the type of '*this'.

> This is however required to allow the simple form of:
>
> parent.createChild<QObject>(); parent.addChild(std::move(pOther));

Ehh?

parent.createChildObject<TypeOfYourObject>();

Implemented, literally, as

template <class Child, class... Args>
Child* QObject::createChildObject(Args&&... args) {
    return new Child(std::forward<Args>(args)..., this);
}

> the workaround is the rather ugly:
>
> parent.createChild<QObject>(&parent);

That's not needed at all.

> I will prepare the following:
>
> * QParentedPointer<Child>
> * qCreateParented<Child>(args);
> * qAddChild(parent, std::unique_ptr<Child>);

No. Make that creation function a member function template of QObject,
and QWidget.


More information about the Development mailing list