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. Qt::MakePointer for creating QPointer as std::make_shared for creating std::shared_ptr
Forum Updated to NodeBB v4.3 + New Features

Qt::MakePointer for creating QPointer as std::make_shared for creating std::shared_ptr

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.4k 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
    Mikhail Matrosov
    wrote on 15 Oct 2014, 19:34 last edited by
    #1

    In modern C++ there is almost no need to use raw new and delete. As Bjarne Stroustrup is saying in his "A Tour of C++": "Avoid ‘‘naked’’ new and delete operations; §4.2.2.". We use standard containers and smart pointers for that, with std::make_shared and std::make_unique functions for smart pointers creation.

    In Qt we also have QSharedPointer and QSharedPointer<T>::create(...) method. But we don't use smart pointers in Qt much, due to parent-driven memory model. And most of QObjects are created with raw new operations. Maybe it is a proper thing to add some C++14-style wrapper for creating QObjects like this:

    @
    namespace Qt
    {
    template<class T, class... Args>
    QPointer<T> MakePointer(Args&&... args)
    {
    T* pObject = new T(std::forward<Args>(args)...);
    Q_ASSERT(pObject->parent() != nullptr);
    return pObject;
    }
    }
    @

    Now, one can safely call Qt::MakePointer to create a QObject and be sure it will not leak due to an assertion for an existing parent. And it will free all the calling code from raw new operations. One can always use raw delete to destroy the object, but he does not have to. And even if he will, it will not lead to dangling pointers problem since QPointer is automatically set to null in this case.

    I'm planning to use this approach in my code. Do you think it is relevant? Are there any drawbacks?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 15 Oct 2014, 21:49 last edited by
      #2

      Hi and welcome to devnet,

      For this kind of design question, you should rather post to the interest mailing list, you'll find there Qt's developers/maintainers. This forum is more user oriented.

      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

      1/2

      15 Oct 2014, 19:34

      • Login

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