Shared Pointer in QT compared to C++11



  • 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 while QScopedPointer ~= std::unique_ptr but let me get away with it

    addWidget(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 double delete


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.