CopperSpice API  1.9.0
QEnableSharedFromThis< T > Class Template Reference

Base class which provides a QSharedPointer for an existing object. More...

Public Methods

QSharedPointer< T > sharedFromThis ()
QSharedPointer< const T > sharedFromThis () const

Detailed Description

template<typename T>
class QEnableSharedFromThis< T >

QEnableSharedFromThis is a base class which provides a mechanism for obtaining a QSharedPointer to the current object.

This class supports retrieving a valid QSharedPointer to this. In C++ "this" is a keyword which evaluates to a raw pointer whose value refers to the current object. If you construct a shared pointer by using the "this" raw pointer the shared pointer will assume responsibilitiy and ownership for the current object. In most cases this will lead to undefined behavior.

To use this class your class must inherit from QEnableSharedFromThis and pass the name of your class as a template parameter.

class MyWidget : public QEnableSharedFromThis<MyWidget>
MyWidget::MyWidget() = default;
QSharedPointer<MyWidget> getSharedPtr() {
return sharedFromThis();
void addWidgetToList(MyWidget *ptr) {
QSharedPointer<MyWidget> tmp = ptr->getSharedPtr();

Method Documentation

template<typename T >
QSharedPointer< T > QEnableSharedFromThis< T >::sharedFromThis ( )

Returns a shared pointer which points to the current object, otherwise this method returns a null pointer.

template<typename T >
QSharedPointer< const T > QEnableSharedFromThis< T >::sharedFromThis ( ) const

Equivalent to calling sharedFromThis(), used when the T is const.