Shared Pointer in QT compared to C++11
Unsolved
General and Desktop
-
Hello,
I am looking for the QT equivalent of the following C++11
formalism:auto ptr = std::make_unique<ClassA>(0,0);
auto ptr = std::make_shared<ClassA>(0,0);When using :
QSharedPointer<ClassA> ptr = QSharedPointer<ClassA>(new ClassA);some methods do not work because the types do not fit, e.g.
addWidget(ptr);Thanks for help.
-
auto ptr = std::make_unique<ClassA>(0,0); //is the same as auto ptr = QSharedPointer<ClassA>::create(0,0);
Above i lied as
QSharedPointer
~=std::shared_ptr
whileQScopedPointer
~=std::unique_ptr
but let me get away with itaddWidget(ptr);
just use
addWidget(ptr.data());
Be aware though that functions like the mentioned (I suppose)
QLayout::addWidget
reparent the widget and will take ownership of the object. Manual/smart deletion can lead to crash due to doubledelete