![]() |
Home · All Namespaces · All Classes · Main Classes · Grouped Classes · Modules · Functions |
The QSharedPointer class holds a strong reference to a shared pointer More...
#include <QSharedPointer>
Note: All the functions in this class are reentrant.
This class was introduced in Qt 4.5.
The QSharedPointer class holds a strong reference to a shared pointer
The QSharedPointer is an automatic, shared pointer in C++. It behaves exactly like a normal pointer for normal purposes, including respect for constness.
QSharedPointer will delete the pointer it is holding when it goes out of scope, provided no other QSharedPointer objects are referencing it.
A QSharedPointer object can be created from a normal pointer, another QSharedPointer object or by promoting a QWeakPointer object to a strong reference.
\section
Thread-safety
QSharedPointer and QWeakPointer are thread-safe and operate atomically on the pointer value. Different threads can also access the same QSharedPointer or QWeakPointer object at the same time without need for locking mechanisms.
It should be noted that, while the pointer value can be accessed in this manner, QSharedPointer and QWeakPointer provide no guarantee about the object being pointed to. Thread-safety and reentrancy rules for that object still apply.
\section
Other pointer classes
Qt also provides two other pointer wrapper classes: QPointer and QSharedDataPointer. They are incompatible with one another, since each has its very different use case.
QSharedPointer holds a shared pointer by means of an external reference count (i.e., a reference counter placed outside the object). Like its name indicates, the pointer value is shared among all instances of QSharedPointer and QWeakPointer. The contents of the object pointed to by the pointer should not considered shared, however: there is only one object. For that reason, QSharedPointer does not provide a way to detach or make copies of the pointed object.
QSharedDataPointer, on the other hand, holds a pointer to shared data (i.e., a class derived from QSharedData). It does so by means of an internal reference count, placed in the QSharedData base class. This class can, therefore, detach based on the type of access made to the data being guarded: if it's a non-const access, it creates a copy atomically for the operation to complete.
QExplicitlySharedDataPointer behaves like QSharedDataPointer, except that it only detaches if QExplicitlySharedDataPointer::detach() is explicitly called.
Finally, QPointer holds a pointer to a QObject-derived object, but it does so weakly. QPointer is similar, in that behaviour, to QWeakPointer: it does not allow you to prevent the object from being destroyed. All you can do is query whether it has been destroyed or not.
See also QSharedDataPointer and QWeakPointer.
Creates a QSharedPointer that points to null (0).
Creates a QSharedPointer that points to ptr. The pointer ptr becomes managed by this QSharedPointer and must not be passed to another QSharedPointer object or deleted outside this object.
Creates a QSharedPointer object that shares other's pointer.
If T is a derived type of the template parameter of this class, QSharedPointer will perform an automatic cast. Otherwise, you will get a compiler error.
Warning: Automatic downcasting is not available on MSVC 6. Use qSharedPointerCast() for portability to MSVC 6.
Creates a QSharedPointer by promoting the weak reference other to strong reference and sharing its pointer.
If T is a derived type of the template parameter of this class, QSharedPointer will perform an automatic cast. Otherwise, you will get a compiler error.
Warning: Automatic downcasting is not available on MSVC 6. Use qSharedPointerCast() for portability to MSVC 6.
Performs a dynamic cast from this pointer's type to X and returns a QSharedPointer that shares the reference. If this function is used to up-cast, then QSharedPointer will perform a dynamic_cast, which means that if the object being pointed by this QSharedPointer is not of type X, the returned object will be null.
Warning: This function is not available in MSVC 6. Use qSharedPointerDynamicCast() for code intended to build on that compiler.
See also isNull().
Performs a static cast from this pointer's type to X and returns a QSharedPointer that shares the reference. This function can be used for up- and for down-casting, but is more useful for up-casting.
Warning: This function is not available in MSVC 6. Use qSharedPointerCast() for code intended to build on that compiler.
Returns a weak reference object that shares the pointer referenced by this object.
Makes this object share other's pointer. The current pointer reference is discarded and, if it was the last, the pointer will be deleted.
If T is a derived type of the template parameter of this class, QSharedPointer will perform an automatic cast. Otherwise, you will get a compiler error.
Warning: Automatic downcasting is not available on MSVC 6. Use qSharedPointerCast() for portability to MSVC 6.
This is an overloaded member function, provided for convenience.
Promotes other to a strong reference and makes this object share a reference to the pointer referenced by it. The current pointer reference is discarded and, if it was the last, the pointer will be deleted.
If T is a derived type of the template parameter of this class, QSharedPointer will perform an automatic cast. Otherwise, you will get a compiler error.
Warning: Automatic downcasting is not available on MSVC 6. Use qSharedPointerCast() for portability to MSVC 6.
This is an overloaded member function, provided for convenience.
This is an overloaded member function, provided for convenience.
Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies) | Trademarks | Qt 4.5.0-tp1 |