Member Function Documentation
QSharedPointer::QSharedPointer ()
Creates a QSharedPointer that points to null (0).
QSharedPointer::QSharedPointer ( T * ptr )
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.
QSharedPointer::QSharedPointer ( T * ptr, Deleter deleter )
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.
The deleter parameter specifies the custom deleter for this object. The custom deleter is called when the strong reference count drops to 0 instead of the operator delete(). This is useful, for instance, for calling deleteLater() in a QObject instead:
static void doDeleteLater(MyObject *obj)
{
obj->deleteLater();
}
void otherFunction()
{
QSharedPointer<MyObject> obj =
QSharedPointer<MyObject>(new MyObject, doDeleteLater);
obj.clear();
}
It is also possible to specify a member function directly, as in:
QSharedPointer<MyObject> obj =
QSharedPointer<MyObject>(new MyObject, &QObject::deleteLater);
See also clear().
QSharedPointer::QSharedPointer ( const QSharedPointer<T> & other )
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.
QSharedPointer::QSharedPointer ( const QWeakPointer<T> & other )
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.
See also QWeakPointer::toStrongRef().
QSharedPointer::~QSharedPointer ()
Destroys this QSharedPointer object. If it is the last reference to the pointer stored, this will delete the pointer as well.
void QSharedPointer::clear ()
Clears this QSharedPointer object, dropping the reference that it may have had to the pointer. If this was the last reference, then the pointer itself will be deleted.
QSharedPointer<X> QSharedPointer::constCast () const
Performs a const_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.
See also isNull() and qSharedPointerConstCast().
T * QSharedPointer::data () const
Returns the value of the pointer referenced by this object.
Note: do not delete the pointer returned by this function or pass it to another function that could delete it, including creating QSharedPointer or QWeakPointer objects.
QSharedPointer<X> QSharedPointer::dynamicCast () const
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.
Note: the template type X must have the same const and volatile qualifiers as the template of this object, or the cast will fail. Use constCast() if you need to drop those qualifiers.
See also qSharedPointerDynamicCast().
bool QSharedPointer::isNull () const
Returns true if this object is holding a reference to a null pointer.
QSharedPointer<X> QSharedPointer::objectCast () const
Performs a qobject_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 qobject_cast, which means that if the object being pointed by this QSharedPointer is not of type X, the returned object will be null.
Note: the template type X must have the same const and volatile qualifiers as the template of this object, or the cast will fail. Use constCast() if you need to drop those qualifiers.
This function was introduced in Qt 4.6.
See also qSharedPointerObjectCast().
QSharedPointer<X> QSharedPointer::staticCast () const
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.
Note: the template type X must have the same const and volatile qualifiers as the template of this object, or the cast will fail. Use constCast() if you need to drop those qualifiers.
See also dynamicCast(), constCast(), and qSharedPointerCast().
QWeakPointer<T> QSharedPointer::toWeakRef () const
Returns a weak reference object that shares the pointer referenced by this object.
See also QWeakPointer::QWeakPointer().
QSharedPointer::operator bool () const
Returns true if this object is not null. This function is suitable for use in if-constructs, like:
if (sharedptr) { ... }
See also isNull().
bool QSharedPointer::operator! () const
Returns true if this object is null. This function is suitable for use in if-constructs, like:
if (!sharedptr) { ... }
See also isNull().
T & QSharedPointer::operator* () const
Provides access to the shared pointer's members.
See also isNull().
T * QSharedPointer::operator-> () const
Provides access to the shared pointer's members.
See also isNull().
QSharedPointer<T> & QSharedPointer::operator= ( const QSharedPointer<T> & other )
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.
QSharedPointer<T> & QSharedPointer::operator= ( const QWeakPointer<T> & other )
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.
Related Non-Members
QSharedPointer<X> qSharedPointerCast ( const QSharedPointer<T> & other )
Returns a shared pointer to the pointer held by other, cast to type X. The types T and X must belong to one hierarchy for the static_cast to succeed.
Note that X must have the same cv-qualifiers (const and volatile) that T has, or the code will fail to compile. Use qSharedPointerConstCast to cast away the constness.
See also QSharedPointer::staticCast(), qSharedPointerDynamicCast(), and qSharedPointerConstCast().
QSharedPointer<X> qSharedPointerConstCast ( const QSharedPointer<T> & other )
Returns a shared pointer to the pointer held by other, cast to type X. The types T and X must belong to one hierarchy for the const_cast to succeed. The const and volatile differences between T and X are ignored.
See also QSharedPointer::constCast(), qSharedPointerCast(), and qSharedPointerDynamicCast().
QSharedPointer<X> qSharedPointerDynamicCast ( const QSharedPointer<T> & other )
Returns a shared pointer to the pointer held by other, using a dynamic cast to type X to obtain an internal pointer of the appropriate type. If the dynamic_cast fails, the object returned will be null.
Note that X must have the same cv-qualifiers (const and volatile) that T has, or the code will fail to compile. Use qSharedPointerConstCast to cast away the constness.
See also QSharedPointer::dynamicCast(), qSharedPointerCast(), and qSharedPointerConstCast().
QSharedPointer<X> qSharedPointerObjectCast ( const QSharedPointer<T> & other )
The qSharedPointerObjectCast function is for casting a shared pointer.
Returns a shared pointer to the pointer held by other, using a qobject_cast() to type X to obtain an internal pointer of the appropriate type. If the qobject_cast fails, the object returned will be null.
Note that X must have the same cv-qualifiers (const and volatile) that T has, or the code will fail to compile. Use qSharedPointerConstCast to cast away the constness.
This function was introduced in Qt 4.6.
See also QSharedPointer::objectCast(), qSharedPointerCast(), and qSharedPointerConstCast().
bool operator!= ( const QSharedPointer<T> & ptr1, const QSharedPointer<X> & ptr2 )
Returns true if the pointer referenced by ptr1 is not the same pointer as that referenced by ptr2.
If ptr2's template parameter is different from ptr1's, QSharedPointer will attempt to perform an automatic static_cast to ensure that the pointers being compared are equal. If ptr2's template parameter is not a base or a derived type from ptr1's, you will get a compiler error.
bool operator!= ( const QSharedPointer<T> & ptr1, const X * ptr2 )
Returns true if the pointer referenced by ptr1 is not the same pointer as ptr2.
If ptr2's type is different from ptr1's, QSharedPointer will attempt to perform an automatic static_cast to ensure that the pointers being compared are equal. If ptr2's type is not a base or a derived type from this ptr1's, you will get a compiler error.
bool operator!= ( const T * ptr1, const QSharedPointer<X> & ptr2 )
Returns true if the pointer ptr1 is not the same pointer as that referenced by ptr2.
If ptr2's template parameter is different from ptr1's type, QSharedPointer will attempt to perform an automatic static_cast to ensure that the pointers being compared are equal. If ptr2's template parameter is not a base or a derived type from ptr1's type, you will get a compiler error.
bool operator== ( const QSharedPointer<T> & ptr1, const QSharedPointer<X> & ptr2 )
Returns true if the pointer referenced by ptr1 is the same pointer as that referenced by ptr2.
If ptr2's template parameter is different from ptr1's, QSharedPointer will attempt to perform an automatic static_cast to ensure that the pointers being compared are equal. If ptr2's template parameter is not a base or a derived type from ptr1's, you will get a compiler error.
bool operator== ( const QSharedPointer<T> & ptr1, const X * ptr2 )
Returns true if the pointer referenced by ptr1 is the same pointer as ptr2.
If ptr2's type is different from ptr1's, QSharedPointer will attempt to perform an automatic static_cast to ensure that the pointers being compared are equal. If ptr2's type is not a base or a derived type from this ptr1's, you will get a compiler error.
bool operator== ( const T * ptr1, const QSharedPointer<X> & ptr2 )
Returns true if the pointer ptr1 is the same pointer as that referenced by ptr2.
If ptr2's template parameter is different from ptr1's type, QSharedPointer will attempt to perform an automatic static_cast to ensure that the pointers being compared are equal. If ptr2's template parameter is not a base or a derived type from ptr1's type, you will get a compiler error.