Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
|
\inmodule QtCore More...
#include <qscopedpointer.h>
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 |
\inmodule QtCore
The QScopedPointer class stores a pointer to a dynamically allocated object, and deletes it upon destruction.
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 70 of file qscopedpointer.h.
T* QScopedPointer< T, Cleanup >::pointer |
Definition at line 145 of file qscopedpointer.h.
|
inlineexplicitnoexcept |
Constructs this QScopedPointer instance and sets its pointer to p.
Definition at line 74 of file qscopedpointer.h.
|
inline |
Destroys this QScopedPointer object.
Delete the object its pointer points to.
Definition at line 78 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 105 of file qscopedpointer.h.
References QScopedPointer< T, Cleanup >::d.
|
inlinenoexcept |
Same as data().
Definition at line 110 of file qscopedpointer.h.
References QScopedPointer< T, Cleanup >::d.
|
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().
|
inlineexplicit |
Returns true
if the contained pointer is not \nullptr.
This function is suitable for use in \tt if-constructs, like:
Definition at line 100 of file qscopedpointer.h.
References QScopedPointer< T, Cleanup >::isNull().
|
inlinenoexcept |
Returns true
if this object refers to \nullptr.
Definition at line 95 of file qscopedpointer.h.
References QScopedPointer< T, Cleanup >::d.
|
inline |
Provides access to the scoped pointer's object.
If the contained pointer is \nullptr, behavior is undefined.
Definition at line 84 of file qscopedpointer.h.
References QScopedPointer< T, Cleanup >::d.
|
inlinenoexcept |
Provides access to the scoped pointer's object.
If the contained pointer is \nullptr, behavior is undefined.
Definition at line 90 of file qscopedpointer.h.
References QScopedPointer< T, Cleanup >::d.
|
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.
|
friend |
Returns true
if lhs and rhs refer to distinct pointers.
Definition at line 152 of file qscopedpointer.h.
|
friend |
Returns true
if lhs refers to a valid (i.e. non-null) pointer.
Definition at line 167 of file qscopedpointer.h.
|
friend |
Returns true
if rhs refers to a valid (i.e. non-null) pointer.
Definition at line 172 of file qscopedpointer.h.
|
friend |
Returns true
if lhs and rhs refer to the same pointer.
Definition at line 147 of file qscopedpointer.h.
|
friend |
Returns true
if lhs refers to \nullptr.
Definition at line 157 of file qscopedpointer.h.
|
friend |
Returns true
if rhs refers to \nullptr.
Definition at line 162 of file qscopedpointer.h.
|
protected |
Definition at line 184 of file qscopedpointer.h.
Referenced by QScopedPointer< T, Cleanup >::data(), QScopedPointer< T, Cleanup >::get(), QScopedPointer< T, Cleanup >::isNull(), QScopedPointer< T, Cleanup >::operator!(), QScopedPointer< T, Cleanup >::operator*(), QScopedPointer< T, Cleanup >::operator->(), and QScopedPointer< T, Cleanup >::reset().