|
These are not member functions
|
bool | operator!= (const QPointer< T > &ptr1, const QPointer< T > &ptr2) |
|
bool | operator!= (const QPointer< T > &ptr1, const T *ptr2) |
|
bool | operator!= (const QPointer< T > &ptr1, T *ptr2) |
|
bool | operator!= (const T *ptr1, const QPointer< T > &ptr2) |
|
bool | operator!= (T *ptr1, const QPointer< T > &ptr2) |
|
bool | operator== (const QPointer< T > &ptr1, const QPointer< T > &ptr2) |
|
bool | operator== (const QPointer< T > &ptr1, const T *ptr2) |
|
bool | operator== (const QPointer< T > &ptr1, T *ptr2) |
|
bool | operator== (const T *ptr1, const QPointer< T > &a ptr2) |
|
bool | operator== (T *ptr1, const QPointer< T > &ptr2) |
|
template<class T>
class QPointer< T >
The QPointer class is a templated class which provides a smart pointer for QObject. QPointer<T> behaves like a normal C++ pointer except it is automatically set to nullptr when the object it points to is destroyed. The T
must inherit from QObject.
Smart pointers are useful when you need to store a pointer to a QObject which is owned by someone else and therefore might be destroyed while you still hold a reference to it. You can safely test the pointer for validity.
CopperSpice also provides QSharedPointer which is a reference-counted shared pointer.
if (label) {
label->show();
}
If the QLabel is deleted, label
will be null instead of an invalid address and the show() method will never be called.
The functions and operators available with a QPointer are the same as those available with a normal pointer. However, pointer arithmetic operators (+
, -
, ++
, and –
) are not allowed.
You can construct or assign a QPointer from a T* or from another QPointer of the same type. You can compare QPointers with each other using operator==() and operator!=(). You can also test for isNull() to ensure it points to a valid address. To dereference, use either the *x
or the x->member
notation.
A QPointer will automatically cast to a T
* so you can freely mix QPointer with raw pointers. If you have a QPointer<QWidget> you can pass it to a function which requires a QWidget *.
Comparison
If a QPointer was pointing to a QWidget (or a subclass of QWidget), the QPointer was nulled by the QWidget destructor. QObject and QWidget were changed so the QPointer is now nulled by the QObject destructor.
In CopperSpice QPointer and QWeakPointer were changed and are now equally efficient. QPointer was changed to use methods in QWeakPointer which are for internal use only. The old guard mechanism in QObject has been removed.
- QPointer has changed to use QWeakPointer which is more efficient and faster
- QPointer is currently not thread safe
The following table contains a list of the smart pointers currently available in CopperSpice.
Category | Classes | Description |
Shared Pointers | QSharedPointer, QWeakPointer | Thread safe pointers, they behave like std::shared_ptr and std::weak_ptr |
Scoped Pointers | QScopedPointer, QScopedArrayPointer | Pointer takes ownership of an object and ensures the object is properly deleted at the end of the pointer's scope
Scoped pointers can not be copied |
Scoped Pointer | QPatternist::AutoPtr | AutoPtr in an internal class based on std::auto_ptr |
Non-Standard | QSharedDataPointer, QExplicitlySharedDataPointer | Pointer makes a copy of the data when changed on a write |
Non-Standard | QPointer | Pointer becomes null when the QObject instance is destroyed |
- See also
- QSharedPointer, QWeakPointer, QObject, QObjectCleanupHandler