Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QEnableSharedFromThis< T > Class Template Reference

\inmodule QtCore More...

#include <qsharedpointer_impl.h>

+ Collaboration diagram for QEnableSharedFromThis< T >:

Public Member Functions

QSharedPointer< T > sharedFromThis ()
 
QSharedPointer< const T > sharedFromThis () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 

Protected Member Functions

 QEnableSharedFromThis ()=default
 
 QEnableSharedFromThis (const QEnableSharedFromThis &)
 
QEnableSharedFromThisoperator= (const QEnableSharedFromThis &)
 

Friends

template<class X >
class QSharedPointer
 

Detailed Description

template<class T>
class QEnableSharedFromThis< T >

\inmodule QtCore

A base class that allows obtaining a QSharedPointer for an object already managed by a shared pointer.

Since
5.4

You can inherit this class when you need to create a QSharedPointer from any instance of a class; for instance, from within the object itself. The key point is that the technique of just returning QSharedPointer<T>(this) cannot be used, because this winds up creating multiple distinct QSharedPointer objects with separate reference counts. For this reason you must never create more than one QSharedPointer from the same raw pointer.

QEnableSharedFromThis defines two member functions called sharedFromThis() that return a QSharedPointer<T> and QSharedPointer<const T>, depending on constness, to this:

class Y: public QEnableSharedFromThis<Y>
{
public:
QSharedPointer<Y> f()
{
return sharedFromThis();
}
};
int main()
{
QSharedPointer<Y> p(new Y());
QSharedPointer<Y> y = p->f();
Q_ASSERT(p == y); // p and q must share ownership
}

It is also possible to get a shared pointer from an object outside of the class itself. This is especially useful in code that provides an interface to scripts, where it is currently not possible to use shared pointers. For example:

class ScriptInterface : public QObject
{
// ...
public slots:
void slotCalledByScript(Y *managedBySharedPointer)
{
QSharedPointer<Y> yPtr = managedBySharedPointer->sharedFromThis();
// Some other code unrelated to scripts that expects a QSharedPointer<Y> ...
}
};

Definition at line 771 of file qsharedpointer_impl.h.

Constructor & Destructor Documentation

◆ QEnableSharedFromThis() [1/2]

template<class T >
QEnableSharedFromThis< T >::QEnableSharedFromThis ( )
protecteddefault

◆ QEnableSharedFromThis() [2/2]

template<class T >
QEnableSharedFromThis< T >::QEnableSharedFromThis ( const QEnableSharedFromThis< T > & )
inlineprotected

Definition at line 775 of file qsharedpointer_impl.h.

Member Function Documentation

◆ operator=()

template<class T >
QEnableSharedFromThis & QEnableSharedFromThis< T >::operator= ( const QEnableSharedFromThis< T > & )
inlineprotected

Definition at line 776 of file qsharedpointer_impl.h.

◆ sharedFromThis() [1/2]

template<class T >
template< class T > QSharedPointer< T > QEnableSharedFromThis< T >::sharedFromThis ( )
inline
Since
5.4

If this (that is, the subclass instance invoking this method) is being managed by a QSharedPointer, returns a shared pointer instance pointing to this; otherwise returns a null QSharedPointer.

Definition at line 779 of file qsharedpointer_impl.h.

◆ sharedFromThis() [2/2]

template<class T >
template< class T > QSharedPointer< const T > QEnableSharedFromThis< T >::sharedFromThis ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since
5.4

Const overload of sharedFromThis().

Definition at line 780 of file qsharedpointer_impl.h.

Friends And Related Symbol Documentation

◆ QSharedPointer

template<class T >
template<class X >
friend class QSharedPointer
friend

Definition at line 783 of file qsharedpointer_impl.h.


The documentation for this class was generated from the following files: