6.0.0-rc4Source

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>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes
Inherited byQScopedArrayPointer

List of all members, including inherited members

Public Functions

QScopedPointer(T *p = nullptr)
T &operator*() const
T *operator->() const
booloperator!() const
operator bool() const
T *data() const
T *get() const
boolisNull() const
voidreset(T *other = nullptr)
T *take()
voidswap(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

[explicit] 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.

See also get() and take().

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.

[explicit] 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.