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

QSharedPointer to interface and sharedFromThis()

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 5.6.3
    • Core: Other
    • None

    Description

      If you have a shared pointer to an interface, then you must assign it a shared pointer to the implementation class in order to be able to use sharedFromThis() inside the class. Example:

      #include <QSharedPointer>
      
      class Interface
      {
      public:
          virtual ~Interface()
          {
          }
      
          virtual bool foo() = 0;
      };
      
      class MyClass : public Interface, public QEnableSharedFromThis<Interface>
      {
      public:
          MyClass()
          {
          }
      
          bool foo() override
          {
              return !sharedFromThis().isNull();
          }
      };
      
      int main()
      {
          QSharedPointer<Interface> instance(new MyClass);
          bool bar = instance->foo(); // Returns false
      
          instance = QSharedPointer<MyClass>::create();
          bar = instance->foo(); // Returns true
      
          instance.reset(new MyClass);
          bar = instance->foo(); // Returns false
      
          return 0;
      }

       

       

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            thiago Thiago Macieira
            tomas.ingo Tomas Ingo
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes