QtBase  v6.3.1
Public Types | Public Member Functions | Protected Attributes | Friends | List of all members
QScopedPointer< T, Cleanup > Class Template Reference

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

#include <qscopedpointer.h>

Inheritance diagram for QScopedPointer< T, Cleanup >:
Inheritance graph
[legend]
Collaboration diagram for QScopedPointer< T, Cleanup >:
Collaboration graph
[legend]

Public Types

typedef Tpointer
 

Public Member Functions

 QScopedPointer (T *p=nullptr) noexcept
 
 ~QScopedPointer ()
 
Toperator* () const
 
Toperator-> () const noexcept
 
bool operator! () const noexcept
 
 operator bool () const
 
Tdata () const noexcept
 
Tget () const noexcept
 
bool isNull () const noexcept
 
void reset (T *other=nullptr) noexcept(noexcept(Cleanup::cleanup(std::declval< T * >())))
 

Protected Attributes

Td
 

Friends

bool operator== (const QScopedPointer< T, Cleanup > &lhs, const QScopedPointer< T, Cleanup > &rhs) noexcept
 
bool operator!= (const QScopedPointer< T, Cleanup > &lhs, 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
 
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 >

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

\inmodule QtCore

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:

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

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:

Definition at line 106 of file qscopedpointer.h.

Member Typedef Documentation

◆ pointer

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

Definition at line 180 of file qscopedpointer.h.

Constructor & Destructor Documentation

◆ QScopedPointer()

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

Constructs this QScopedPointer instance and sets its pointer to p.

Definition at line 109 of file qscopedpointer.h.

◆ ~QScopedPointer()

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

Destroys this QScopedPointer object. Delete the object its pointer points to.

Definition at line 113 of file qscopedpointer.h.

Member Function Documentation

◆ data()

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
template< typename T, typename Cleanup > 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 140 of file qscopedpointer.h.

Here is the caller graph for this function:

◆ get()

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

Same as data().

Definition at line 145 of file qscopedpointer.h.

Here is the caller graph for this function:

◆ isNull()

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

Returns true if this object refers to \nullptr.

Definition at line 150 of file qscopedpointer.h.

Here is the caller graph for this function:

◆ operator bool()

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
template< typename T, typename Cleanup > 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:

See also
isNull()

Definition at line 135 of file qscopedpointer.h.

◆ operator!()

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

Returns true if this object refers to \nullptr.

See also
isNull()

Definition at line 130 of file qscopedpointer.h.

◆ operator*()

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
template< typename T, typename Cleanup > 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 119 of file qscopedpointer.h.

◆ operator->()

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
template< typename T, typename Cleanup > 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 125 of file qscopedpointer.h.

◆ reset()

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
template< typename T, typename Cleanup > 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 155 of file qscopedpointer.h.

Here is the call graph for this function:
Here is the caller graph for this function:

Friends And Related Function Documentation

◆ operator!= [1/3]

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
template< typename T, typename Cleanup > bool QScopedPointer< T, Cleanup >::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 187 of file qscopedpointer.h.

◆ operator!= [2/3]

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
template< typename T, typename Cleanup > bool QScopedPointer< T, Cleanup >::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 202 of file qscopedpointer.h.

◆ operator!= [3/3]

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
template< typename T, typename Cleanup > bool QScopedPointer< T, Cleanup >::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 207 of file qscopedpointer.h.

◆ operator== [1/3]

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
template< typename T, typename Cleanup > bool QScopedPointer< T, Cleanup >::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 182 of file qscopedpointer.h.

◆ operator== [2/3]

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
template< typename T, typename Cleanup > bool QScopedPointer< T, Cleanup >::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 192 of file qscopedpointer.h.

◆ operator== [3/3]

template<typename T , typename Cleanup = QScopedPointerDeleter<T>>
template< typename T, typename Cleanup > bool QScopedPointer< T, Cleanup >::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 197 of file qscopedpointer.h.

Member Data Documentation

◆ d

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

Definition at line 219 of file qscopedpointer.h.


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