QtBase
v6.3.1
|
The QScopedPointer class stores a pointer to a dynamically allocated object, and deletes it upon destruction. More...
#include <qscopedpointer.h>
Public Types | |
typedef T * | pointer |
Public Member Functions | |
QScopedPointer (T *p=nullptr) noexcept | |
~QScopedPointer () | |
T & | operator* () const |
T * | operator-> () const noexcept |
bool | operator! () const noexcept |
operator bool () const | |
T * | data () const noexcept |
T * | get () const noexcept |
bool | isNull () const noexcept |
void | reset (T *other=nullptr) noexcept(noexcept(Cleanup::cleanup(std::declval< T * >()))) |
Protected Attributes | |
T * | d |
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 |
The QScopedPointer class stores a pointer to a dynamically allocated object, and deletes it upon destruction.
\inmodule QtCore
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.
QScopedPointer< T, Cleanup >::pointer |
Definition at line 180 of file qscopedpointer.h.
|
inlineexplicitnoexcept |
Constructs this QScopedPointer instance and sets its pointer to p.
Definition at line 109 of file qscopedpointer.h.
|
inline |
Destroys this QScopedPointer object. Delete the object its pointer points to.
Definition at line 113 of file qscopedpointer.h.
|
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.
|
inlinenoexcept |
Same as data().
Definition at line 145 of file qscopedpointer.h.
|
inlinenoexcept |
Returns true
if this object refers to \nullptr.
Definition at line 150 of file qscopedpointer.h.
|
inlineexplicit |
Returns true
if the contained pointer is not \nullptr. This function is suitable for use in \tt if-constructs, like:
Definition at line 135 of file qscopedpointer.h.
|
inlinenoexcept |
Returns true
if this object refers to \nullptr.
Definition at line 130 of file qscopedpointer.h.
|
inline |
Provides access to the scoped pointer's object.
If the contained pointer is \nullptr, behavior is undefined.
Definition at line 119 of file qscopedpointer.h.
|
inlinenoexcept |
Provides access to the scoped pointer's object.
If the contained pointer is \nullptr, behavior is undefined.
Definition at line 125 of file qscopedpointer.h.
|
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.
|
friend |
Returns true
if lhs and rhs refer to distinct pointers.
Definition at line 187 of file qscopedpointer.h.
|
friend |
Returns true
if lhs refers to a valid (i.e. non-null) pointer.
Definition at line 202 of file qscopedpointer.h.
|
friend |
Returns true
if rhs refers to a valid (i.e. non-null) pointer.
Definition at line 207 of file qscopedpointer.h.
|
friend |
Returns true
if lhs and rhs refer to the same pointer.
Definition at line 182 of file qscopedpointer.h.
|
friend |
Returns true
if lhs refers to \nullptr.
Definition at line 192 of file qscopedpointer.h.
|
friend |
Returns true
if rhs refers to \nullptr.
Definition at line 197 of file qscopedpointer.h.
|
protected |
Definition at line 219 of file qscopedpointer.h.