Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-7287

Have an equivalent to boost::enable_shared_from_this for QSharedPointer

XMLWordPrintable

    • Icon: Suggestion Suggestion
    • Resolution: Done
    • Icon: P3: Somewhat important P3: Somewhat important
    • 5.4.0
    • 4.6.0
    • None
    • All

    • 90a68926f311ac5bb8f7c53ac8370f9ff7f12c08

      When using boost::shared_ptr, you can have your classes derive from boost::enable_shared_from_this, which then allows you to have a method that returns the shared pointer associated with the object. For example, one can have a class like this:

      class MyClass : public boost::enable_shared_from_this
      {
      public:
      boost::shared_ptr<MyClass> getSharedThis() const

      { return shared_from_this(); }

      }

      Note that the ref counter is updated with the creation of the new pointer.

      In Qt, one could have this method always present in QObject.

      Other implementations are possible, such as having:
      QSharedPointer<MyClass> sPtr = qSharedFromPointer(ptr);

      Etc...

      Thanks for considering, as right now I am stuck using a combination of QPointer and boost::shared_ptr to get things working.

        For Gerrit Dashboard: QTBUG-7287
        # Subject Branch Project Status CR V

            peppe Giuseppe D'Angelo
            sirmont Jean-Olivier Racine
            Votes:
            12 Vote for this issue
            Watchers:
            14 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes