As far as I read in the documentation in order to be able to obtain QSharedPointer from within the object itself it is necessary to inherit from QEnableSharedFromThis. This makes me think of the case when the class must inherit from another class, e.g. QObject:

Qt Code:
  1. class MyObject : public QObject, public QEnableSharedFromThis<MyObject>
  2. {
  3. public:
  4. void addChild(MyObject *child)
  5. {
  6. child->parent = sharedFromThis();
  7. }
  8.  
  9. private:
  10. QWeakPointer<MyObject> parent;
  11. };
  12.  
  13. QSharedPointer<MyObject> myObject(new MyObject);
  14. myObject->addChild(QSharedPointer<MyObject>::create().data());
To copy to clipboard, switch view to plain text mode 

Is it a correct approach? Multiple inheritance is often discouraged, but I don't see another way of doing this.