Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPointer vs std::unique_ptr
Forum Updated to NodeBB v4.3 + New Features

QPointer vs std::unique_ptr

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 5.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maximus
    wrote on last edited by
    #1

    Hello guys,

    I recently switched from Java to C++, as you may guess I had lot of problem with pointers at first.
    I learned to use std::unique_ptr and std::shared_ptr for different use now and now i'm doing fine.

    I just learned that there is a class QPointer that is similar to that.
    But your object must inherit Q_Object in order to use it.

    My question : Is it worth it to use QPointer or does std::unique_ptr does that job fine and/or better?
    I'm no C++ guru so I don't know about speed/efficiency but the QPointer syntax looks more simple to me.

    Thanks in advance!


    Free Indoor Cycling Software - https://maximumtrainer.com

    1 Reply Last reply
    1
    • T Offline
      T Offline
      t3685
      wrote on last edited by
      #2

      QPointer and std::unique_ptr are not equivalent.

      QPointer just lets you know the object it is pointing is destroyed.

      std::unique_ptr deletes the object for you on destruction.

      What you are looking for is QScopedPointer.

      1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        If std::unique_ptr does the job then go on, nothings wrong using it. There's no need to create a QObject derived class only to use QPointer if you already have something working.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved