QScopedPointer Class
template <typename T, typename Cleanup = QScopedPointerDeleter<T>> class QScopedPointer
The QScopedPointer class stores a pointer to a dynamically allocated object and deletes it on destruction. More…
| Header | #include <QScopedPointer> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
| Inherited by | QScopedArrayPointer |
List of all members, including inherited members
Public Functions
| QScopedPointer(T *p = nullptr) | |
| T & | operator*() const |
| T * | operator->() const |
| bool | operator!() const |
| operator bool() const | |
| T * | data() const |
| T * | get() const |
| bool | isNull() const |
| void | reset(T *other = nullptr) |
| T * | take() |
| void | swap(QScopedPointer &o) |
Detailed Description
A QScopedPointer owns the object it points to and deletes it, through the Cleanup handler, when it goes out of scope or is reset. It cannot be copied.
QScopedPointer<QFile> file(new QFile("data.txt"));
file->open(QIODevice::ReadOnly);
See also QScopedArrayPointer and QSharedPointer.
Differences from Qt
QScopedPointer is built on std::unique_ptr and is not movable. The Cleanup handler must be usable as a std::unique_ptr deleter (see QScopedPointerDeleter). There are no comparison operators and no free swap() function.
Member Function Documentation
QScopedPointer::QScopedPointer(T *p = nullptr)
Constructs a scoped pointer that owns p. The default is a null pointer.
T * QScopedPointer::data() const
Returns the pointer to the object, without giving up ownership.
T * QScopedPointer::get() const
Returns the pointer to the object. This is the same as data().
bool QScopedPointer::isNull() const
Returns true if the pointer is null; otherwise returns false.
QScopedPointer::operator bool() const
Returns true if the pointer is not null; otherwise returns false.
bool QScopedPointer::operator!() const
Returns true if the pointer is null; otherwise returns false.
T & QScopedPointer::operator*() const
Returns a reference to the object. The pointer must not be null.
T * QScopedPointer::operator->() const
Returns the pointer to the object.
void QScopedPointer::reset(T *other = nullptr)
Deletes the current object and takes ownership of other. The default is a null pointer.
void QScopedPointer::swap(QScopedPointer &o)
Swaps this pointer with o.
T * QScopedPointer::take()
Returns the pointer to the object and releases ownership, so the caller must delete it. The scoped pointer becomes null.