QWeakPointer Class

The QWeakPointer class holds a weak reference to a shared pointer. More...

Header: #include <QWeakPointer>
qmake: QT += core
Since: Qt 4.5

Note: All functions in this class are reentrant.

Public Functions

QWeakPointer()
QWeakPointer(const QWeakPointer<T> &other)
QWeakPointer(const QSharedPointer<T> &other)
QWeakPointer(const int *other)
~QWeakPointer()
void clear()
T *data() const
bool isNull() const
QSharedPointer<T> lock() const
void swap(QWeakPointer<T> &other)
QSharedPointer<T> toStrongRef() const
bool operator bool() const
bool operator!() const
QWeakPointer<T> &operator=(const QWeakPointer<T> &other)
QWeakPointer<T> &operator=(const QSharedPointer<T> &other)
QWeakPointer<T> &operator=(const int *other)

Detailed Description

The QWeakPointer class holds a weak reference to a shared pointer.

The QWeakPointer is an automatic weak reference to a pointer in C++. It cannot be used to dereference the pointer directly, but it can be used to verify if the pointer has been deleted or not in another context.

QWeakPointer objects can only be created by assignment from a QSharedPointer.

It's important to note that QWeakPointer provides no automatic casting operators to prevent mistakes from happening. Even though QWeakPointer tracks a pointer, it should not be considered a pointer itself, since it doesn't guarantee that the pointed object remains valid.

Therefore, to access the pointer that QWeakPointer is tracking, you must first promote it to QSharedPointer and verify if the resulting object is null or not. QSharedPointer guarantees that the object isn't deleted, so if you obtain a non-null object, you may use the pointer. See QWeakPointer::toStrongRef() for an example.

QWeakPointer also provides the QWeakPointer::data() method that returns the tracked pointer without ensuring that it remains valid. This function is provided if you can guarantee by external means that the object will not get deleted (or if you only need the pointer value) and the cost of creating a QSharedPointer using toStrongRef() is too high.

See also QSharedPointer and QScopedPointer.

Member Function Documentation

QWeakPointer::QWeakPointer()

Default constructs an instance of QWeakPointer.

QWeakPointer::QWeakPointer(const QWeakPointer<T> &other)

Default constructs an instance of QWeakPointer.

QWeakPointer::QWeakPointer(const QSharedPointer<T> &other)

Default constructs an instance of QWeakPointer.

QWeakPointer::QWeakPointer(const int *other)

Default constructs an instance of QWeakPointer.

QWeakPointer::~QWeakPointer()

Destroys the instance of QWeakPointer.

void QWeakPointer::clear()

T *QWeakPointer::data() const

bool QWeakPointer::isNull() const

QSharedPointer<T> QWeakPointer::lock() const

void QWeakPointer::swap(QWeakPointer<T> &other)

QSharedPointer<T> QWeakPointer::toStrongRef() const

bool QWeakPointer::operator bool() const

bool QWeakPointer::operator!() const

QWeakPointer<T> &QWeakPointer::operator=(const QWeakPointer<T> &other)

Copy-assignment operator.

QWeakPointer<T> &QWeakPointer::operator=(const QSharedPointer<T> &other)

Copy-assignment operator.

QWeakPointer<T> &QWeakPointer::operator=(const int *other)