Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QScopedPointer< T, Cleanup > Class Template Reference

\inmodule QtCore More...

#include <qscopedpointer.h>

+ Inheritance diagram for QScopedPointer< T, Cleanup >:
+ Collaboration diagram for QScopedPointer< T, Cleanup >:

Public Types

typedef T * pointer
 

Public Member Functions

Q_NODISCARD_CTOR QScopedPointer (T *p=nullptr) noexcept
 Constructs this QScopedPointer instance and sets its pointer to p.
 
 ~QScopedPointer ()
 Destroys this QScopedPointer object.
 
T & operator* () const
 Provides access to the scoped pointer's object.
 
T * operator-> () const noexcept
 Provides access to the scoped pointer's object.
 
bool operator! () const noexcept
 Returns true if this object refers to \nullptr.
 
 operator bool () const
 Returns true if the contained pointer is not \nullptr.
 
T * data () const noexcept
 Returns the value of the pointer referenced by this object.
 
T * get () const noexcept
 
bool isNull () const noexcept
 Returns true if this object refers to \nullptr.
 
void reset (T *other=nullptr) noexcept(noexcept(Cleanup::cleanup(std::declval< T * >())))
 Deletes the existing object it is pointing to (if any), and sets its pointer to other.
 

Protected Attributes

T * d
 

Friends

bool operator== (const QScopedPointer< T, Cleanup > &lhs, const QScopedPointer< T, Cleanup > &rhs) noexcept
 Returns true if lhs and rhs refer to the same pointer.
 
bool operator!= (const QScopedPointer< T, Cleanup > &lhs, const QScopedPointer< T, Cleanup > &rhs) noexcept
 Returns true if lhs and rhs refer to distinct pointers.
 
bool operator== (const QScopedPointer< T, Cleanup > &lhs, std::nullptr_t) noexcept
 
bool operator== (std::nullptr_t, const QScopedPointer< T, Cleanup > &rhs) noexcept
 
bool operator!= (const QScopedPointer< T, Cleanup > &lhs, std::nullptr_t) noexcept
 
bool operator!= (std::nullptr_t, const QScopedPointer< T, Cleanup > &rhs) noexcept
 

Detailed Description

template<typename T, typename Cleanup = QScopedPointerDeleter<T>>
class QScopedPointer< T, Cleanup >

\inmodule QtCore

The QScopedPointer class stores a pointer to a dynamically allocated object, and deletes it upon destruction.

Since
4.6 \reentrant

Managing heap allocated objects manually is hard and error prone, with the common result that code leaks memory and is hard to maintain. QScopedPointer is a small utility class that heavily simplifies this by assigning stack-based memory ownership to heap allocations, more generally called resource acquisition is initialization(RAII).

QScopedPointer guarantees that the object pointed to will get deleted when the current scope disappears.

Consider this function which does heap allocations, and has various exit points:

void myFunction(bool useSubClass)
{
MyClass *p = useSubClass ? new MyClass() : new MySubClass;
QIODevice *device = handsOverOwnership();
if (m_value > 3) {
delete p;
delete device;
return;
}
try {
process(device);
}
catch (...) {
delete p;
delete device;
throw;
}
delete p;
delete device;
}

It's encumbered by the manual delete calls. With QScopedPointer, the code can be simplified to:

void myFunction(bool useSubClass)
{
// assuming that MyClass has a virtual destructor
QScopedPointer<MyClass> p(useSubClass ? new MyClass() : new MySubClass);
QScopedPointer<QIODevice> device(handsOverOwnership());
if (m_value > 3)
return;
process(device);
}

The code the compiler generates for QScopedPointer is the same as when writing it manually. Code that makes use of delete are candidates for QScopedPointer usage (and if not, possibly another type of smart pointer such as QSharedPointer). QScopedPointer intentionally has no copy constructor or assignment operator, such that ownership and lifetime is clearly communicated.

The const qualification on a regular C++ pointer can also be expressed with a QScopedPointer:

const QWidget *const p = new QWidget();
// is equivalent to:
QWidget *const p = new QWidget();
// is equivalent to:
const QWidget *p = new QWidget();
// is equivalent to:

Definition at line 70 of file qscopedpointer.h.

Member Typedef Documentation

◆ pointer

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
T* QScopedPointer< T, Cleanup >::pointer

Definition at line 145 of file qscopedpointer.h.

Constructor & Destructor Documentation

◆ QScopedPointer()

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
Q_NODISCARD_CTOR QScopedPointer< T, Cleanup >::QScopedPointer ( T * p = nullptr)
inlineexplicitnoexcept

Constructs this QScopedPointer instance and sets its pointer to p.

Definition at line 74 of file qscopedpointer.h.

◆ ~QScopedPointer()

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
QScopedPointer< T, Cleanup >::~QScopedPointer ( )
inline

Destroys this QScopedPointer object.

Delete the object its pointer points to.

Definition at line 78 of file qscopedpointer.h.

Member Function Documentation

◆ data()

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
T * QScopedPointer< T, Cleanup >::data ( ) const
inlinenoexcept

Returns the value of the pointer referenced by this object.

QScopedPointer still owns the object pointed to.

Definition at line 105 of file qscopedpointer.h.

References QScopedPointer< T, Cleanup >::d.

◆ get()

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
T * QScopedPointer< T, Cleanup >::get ( ) const
inlinenoexcept
Since
5.11

Same as data().

Definition at line 110 of file qscopedpointer.h.

References QScopedPointer< T, Cleanup >::d.

◆ isNull()

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
bool QScopedPointer< T, Cleanup >::isNull ( ) const
inlinenoexcept

Returns true if this object refers to \nullptr.

Definition at line 115 of file qscopedpointer.h.

References QScopedPointer< T, Cleanup >::d.

Referenced by QScopedPointer< T, Cleanup >::operator bool().

+ Here is the caller graph for this function:

◆ operator bool()

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
QScopedPointer< T, Cleanup >::operator bool ( ) const
inlineexplicit

Returns true if the contained pointer is not \nullptr.

This function is suitable for use in \tt if-constructs, like:

if (scopedPointer) {
...
}
See also
isNull()

Definition at line 100 of file qscopedpointer.h.

References QScopedPointer< T, Cleanup >::isNull().

+ Here is the call graph for this function:

◆ operator!()

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
bool QScopedPointer< T, Cleanup >::operator! ( ) const
inlinenoexcept

Returns true if this object refers to \nullptr.

See also
isNull()

Definition at line 95 of file qscopedpointer.h.

References QScopedPointer< T, Cleanup >::d.

◆ operator*()

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
T & QScopedPointer< T, Cleanup >::operator* ( ) const
inline

Provides access to the scoped pointer's object.

If the contained pointer is \nullptr, behavior is undefined.

See also
isNull()

Definition at line 84 of file qscopedpointer.h.

References QScopedPointer< T, Cleanup >::d.

◆ operator->()

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
T * QScopedPointer< T, Cleanup >::operator-> ( ) const
inlinenoexcept

Provides access to the scoped pointer's object.

If the contained pointer is \nullptr, behavior is undefined.

See also
isNull()

Definition at line 90 of file qscopedpointer.h.

References QScopedPointer< T, Cleanup >::d.

◆ reset()

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
void QScopedPointer< T, Cleanup >::reset ( T * other = nullptr)
inlinenoexcept

Deletes the existing object it is pointing to (if any), and sets its pointer to other.

QScopedPointer now owns other and will delete it in its destructor.

Definition at line 120 of file qscopedpointer.h.

References QScopedPointer< T, Cleanup >::d.

Friends And Related Symbol Documentation

◆ operator!= [1/3]

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
bool operator!= ( const QScopedPointer< T, Cleanup > & lhs,
const QScopedPointer< T, Cleanup > & rhs )
friend

Returns true if lhs and rhs refer to distinct pointers.

Definition at line 152 of file qscopedpointer.h.

◆ operator!= [2/3]

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
bool operator!= ( const QScopedPointer< T, Cleanup > & lhs,
std::nullptr_t  )
friend
Since
5.8

Returns true if lhs refers to a valid (i.e. non-null) pointer.

See also
QScopedPointer::isNull()

Definition at line 167 of file qscopedpointer.h.

◆ operator!= [3/3]

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
bool operator!= ( std::nullptr_t ,
const QScopedPointer< T, Cleanup > & rhs )
friend
Since
5.8

Returns true if rhs refers to a valid (i.e. non-null) pointer.

See also
QScopedPointer::isNull()

Definition at line 172 of file qscopedpointer.h.

◆ operator== [1/3]

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
bool operator== ( const QScopedPointer< T, Cleanup > & lhs,
const QScopedPointer< T, Cleanup > & rhs )
friend

Returns true if lhs and rhs refer to the same pointer.

Definition at line 147 of file qscopedpointer.h.

◆ operator== [2/3]

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
bool operator== ( const QScopedPointer< T, Cleanup > & lhs,
std::nullptr_t  )
friend
Since
5.8

Returns true if lhs refers to \nullptr.

See also
QScopedPointer::isNull()

Definition at line 157 of file qscopedpointer.h.

◆ operator== [3/3]

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
bool operator== ( std::nullptr_t ,
const QScopedPointer< T, Cleanup > & rhs )
friend
Since
5.8

Returns true if rhs refers to \nullptr.

See also
QScopedPointer::isNull()

Definition at line 162 of file qscopedpointer.h.

Member Data Documentation

◆ d


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