QSharedPointer vs std::shared_ptr



  • Hey,

    since C++ 11 we can choose (in a Qt dev environment) between QSharedPointer and std::shared_ptr. Can anybody tell me if i should prefer std::shared_ptr or QSharedPointer and why - if i create a new class.

    Here is what i know now:

    • QSharedPointer can be used in a non C++ 11 compiler (pro QSharedPointer)
    • std::shared_ptr is moveable, QSharedPointer not (pro std::shared_ptr)
    • std::shared_ptr reference is boost::shared_ptr (pro std::shared_ptr)

    Thanks in advance
    Tonka


  • Moderators

    It mostly depends on your needs. QSharedPointer is obviously better with Qt types, as it provides convenience functions like qSharedPointerObjectCast.

    It's also older than shared_ptr so it doesn't have move semantics treatment, but the benefits of moving a shared pointer are minor IMHO. I guess it's possible that it would get an update in Qt6.

    In most cases it's a preference choice. I used both intensively in the past and have never met a scenario when one would fit better than the other, except for the mentioned casting benefits.



  • thx for the answer. I will prefer QSharedPointer over std::shared_ptr


Log in to reply
 

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