QSharedPointer Class | Qt Core | Qt 6.10.3
Description: The QSharedPointer class holds a strong reference to a shared pointer.

- Qt 6.10
- Qt Core
- C++ Classes
- QSharedPointer
- Qt 6.10.3 Reference Documentation

# QSharedPointer Class

template <typename T> class QSharedPointer

The QSharedPointer class holds a strong reference to a shared pointer. More...

Header: | `#include <QSharedPointer>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Core)` `target_link_libraries(mytarget PRIVATE Qt6::Core)`
qmake: | `QT += core`

- List of all members, including inherited members

This class is strongly comparable.

This class is strongly comparable with QSharedPointer<X>, X*, and std::nullptr_t.

Where X and T are compatible types, which means that they are either the same or one is a base type of the other.

Note: All functions in this class are reentrant.

## Public Functions

| QSharedPointer()
| QSharedPointer(QSharedPointer<X> &&other)
| QSharedPointer(X *ptr)
| QSharedPointer(const QWeakPointer<T> &other)
| QSharedPointer(std::nullptr_t)
| QSharedPointer(X *ptr, Deleter d)
| QSharedPointer(std::nullptr_t, Deleter d)
| QSharedPointer(const QSharedPointer<T> &other)
| QSharedPointer(QSharedPointer<T> &&other)
| ~QSharedPointer()
void | clear()
QSharedPointer<X> | constCast() const &
`(since 6.9)` QSharedPointer<X> | constCast() &&
T * | data() const
QSharedPointer<X> | dynamicCast() const &
`(since 6.9)` QSharedPointer<X> | dynamicCast() &&
T * | get() const
bool | isNull() const
QSharedPointer<X> | objectCast() const &
`(since 6.9)` QSharedPointer<X> | objectCast() &&
`(since 6.7)` bool | owner_before(const QSharedPointer<X> &other) const
`(since 6.7)` bool | owner_before(const QWeakPointer<X> &other) const
`(since 6.7)` bool | owner_equal(const QSharedPointer<X> &other) const
`(since 6.7)` bool | owner_equal(const QWeakPointer<X> &other) const
`(since 6.7)` size_t | owner_hash() const
void | reset()
void | reset(T *t)
void | reset(T *t, Deleter deleter)
QSharedPointer<X> | staticCast() const &
`(since 6.9)` QSharedPointer<X> | staticCast() &&
void | swap(QSharedPointer<T> &other)
QWeakPointer<T> | toWeakRef() const
bool | operator bool() const
bool | operator!() const
T & | operator*() const
T * | operator->() const
QSharedPointer<T> & | operator=(QSharedPointer<T> &&other)
QSharedPointer<T> & | operator=(QSharedPointer<X> &&other)
QSharedPointer<T> & | operator=(const QSharedPointer<T> &other)
QSharedPointer<T> & | operator=(const QWeakPointer<T> &other)

## Static Public Members

QSharedPointer<T> | create(Args &&... args)

## Related Non-Members

size_t | qHash(const QSharedPointer<T> &key, size_t seed = 0)
QSharedPointer<X> | qSharedPointerCast(const QSharedPointer<T> &other)
`(since 6.9)` QSharedPointer<X> | qSharedPointerCast(QSharedPointer<T> &&other)
QSharedPointer<X> | qSharedPointerCast(const QWeakPointer<T> &other)
QSharedPointer<X> | qSharedPointerConstCast(const QSharedPointer<T> &src)
`(since 6.9)` QSharedPointer<X> | qSharedPointerConstCast(QSharedPointer<T> &&src)
QSharedPointer<X> | qSharedPointerConstCast(const QWeakPointer<T> &src)
QSharedPointer<X> | qSharedPointerDynamicCast(const QSharedPointer<T> &src)
`(since 6.9)` QSharedPointer<X> | qSharedPointerDynamicCast(QSharedPointer<T> &&src)
QSharedPointer<X> | qSharedPointerDynamicCast(const QWeakPointer<T> &src)
QSharedPointer<X> | qSharedPointerObjectCast(const QSharedPointer<T> &src)
`(since 6.9)` QSharedPointer<X> | qSharedPointerObjectCast(QSharedPointer<T> &&src)
QSharedPointer<X> | qSharedPointerObjectCast(const QWeakPointer<T> &src)
std::shared_ptr<X> | qSharedPointerObjectCast(const std::shared_ptr<T> &src)
std::shared_ptr<X> | qSharedPointerObjectCast(std::shared_ptr<T> &&src)
std::shared_ptr<X> | qobject_pointer_cast(const std::shared_ptr<T> &src)
std::shared_ptr<X> | qobject_pointer_cast(std::shared_ptr<T> &&src)
bool | operator!=(const QSharedPointer<T> &lhs, const QSharedPointer<X> &rhs)
bool | operator!=(const QSharedPointer<T> &lhs, const X *rhs)
bool | operator!=(const QSharedPointer<T> &lhs, std::nullptr_t)
bool | operator!=(const T *lhs, const QSharedPointer<X> &rhs)
bool | operator!=(std::nullptr_t, const QSharedPointer<T> &rhs)
QDebug | operator<<(QDebug debug, const QSharedPointer<T> &ptr)
bool | operator==(const QSharedPointer<T> &lhs, const QSharedPointer<X> &rhs)
bool | operator==(const QSharedPointer<T> &lhs, const X *rhs)
bool | operator==(const QSharedPointer<T> &lhs, std::nullptr_t)
bool | operator==(const T *lhs, const QSharedPointer<X> &rhs)
bool | operator==(std::nullptr_t, const QSharedPointer<T> &rhs)

## Detailed Description

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.

### Thread-Safety

QSharedPointer and QWeakPointer are reentrant classes. This means that, in general, a given QSharedPointer or QWeakPointer object cannot be accessed by multiple threads at the same time without synchronization.

Different QSharedPointer and QWeakPointer objects can safely be accessed by multiple threads at the same time. This includes the case where they hold pointers to the same object; the reference counting mechanism is atomic, and no manual synchronization is required.

It should be noted that, while the pointer value can be accessed in this manner (that is, by multiple threads at the same time, without synchronization), QSharedPointer and QWeakPointer provide no guarantee about the object being pointed to. The specific thread-safety and reentrancy rules for that object still apply.

### 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 be 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 is a variant of QSharedDataPointer, except that it only detaches if QExplicitlySharedDataPointer::detach() is explicitly called (hence the name).

QScopedPointer simply holds a pointer to a heap allocated object and deletes it in its destructor. This class is useful when an object needs to be heap allocated and deleted, but no more. QScopedPointer is lightweight, it makes no use of additional structure or reference counting.

Finally, QPointer holds a pointer to a QObject-derived object, but it does so weakly. QWeakPointer has the same functionality, but its use for that function is deprecated.

### Optional Pointer Tracking

A feature of QSharedPointer that can be enabled at compile-time for debugging purposes is a pointer tracking mechanism. When enabled, QSharedPointer registers in a global set all the pointers that it tracks. This allows one to catch mistakes like assigning the same pointer to two QSharedPointer objects.

This function is enabled by defining the `QT_SHAREDPOINTER_TRACK_POINTERS` macro before including the QSharedPointer header.

It is safe to use this feature even with code compiled without the feature. QSharedPointer will ensure that the pointer is removed from the tracker even from code compiled without pointer tracking.

Note, however, that the pointer tracking feature has limitations on multiple- or virtual-inheritance (that is, in cases where two different pointer addresses can refer to the same object). In that case, if a pointer is cast to a different type and its value changes, QSharedPointer's pointer tracking mechanism may fail to detect that the object being tracked is the same.

See also QSharedDataPointer, QWeakPointer, QScopedPointer, and QEnableSharedFromThis.

## Member Function Documentation

### `[noexcept, since 6.7]` template <typename X> bool QSharedPointer::owner_before(const QSharedPointer<X> &other) const

### `[noexcept, since 6.7]` template <typename X> bool QSharedPointer::owner_before(const QWeakPointer<X> &other) const

Returns `true` if and only if this smart pointer precedes other in an implementation-defined owner-based ordering. The ordering is such that two smart pointers are considered equivalent if they are both empty or if they both own the same object (even if their apparent type and pointer are different).

These functions were introduced in Qt 6.7.

See also owner_equal.

### `[noexcept, since 6.7]` template <typename X> bool QSharedPointer::owner_equal(const QSharedPointer<X> &other) const

### `[noexcept, since 6.7]` template <typename X> bool QSharedPointer::owner_equal(const QWeakPointer<X> &other) const

Returns `true` if and only if this smart pointer and other share ownership.

These functions were introduced in Qt 6.7.

See also owner_before and owner_hash.

### `[noexcept, since 6.7]` size_t QSharedPointer::owner_hash() const

Returns a owner-based hash value for this smart pointer object. Smart pointers that compare equal (as per `owner_equal`) will have an identical owner-based hash.

These function were introduced in Qt 6.7.

See also owner_equal.

### QSharedPointer::QSharedPointer()

Creates a QSharedPointer that is null (the object is holding a reference to `nullptr`).

### `[noexcept]` template <typename X> QSharedPointer::QSharedPointer(QSharedPointer<X> &&other)

Move-constructs a QSharedPointer instance, making it point at the same object that other was pointing to.

#### Constraints

Participates in overload resolution only if `X*` implicitly converts to `T*`.

### `[explicit]` template <typename X> QSharedPointer::QSharedPointer(X *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.

Since Qt 5.8, when the last reference to this QSharedPointer gets destroyed, ptr will be deleted by calling `X`'s destructor (even if `X` is not the same as QSharedPointer's template parameter `T`). Previously, the destructor for `T` was called.

### 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(std::nullptr_t)

Creates a QSharedPointer that is null. This is equivalent to the QSharedPointer default constructor.

### template <typename X, typename Deleter> QSharedPointer::QSharedPointer(X *ptr, Deleter d)

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 d specifies the custom deleter for this object. The custom deleter is called, instead of the operator delete(), when the strong reference count drops to 0. This is useful, for instance, for calling deleteLater() on a QObject instead:

         static void doDeleteLater(MyObject *obj)
         {
             obj->deleteLater();
         }

         void otherFunction()
         {
             QSharedPointer<MyObject> obj =
                 QSharedPointer<MyObject>(new MyObject, doDeleteLater);

             // continue using obj
             obj.clear();    // calls obj->deleteLater();
         }

Note that the custom deleter function will be called with a pointer to type `X`, even if the QSharedPointer template parameter `T` is not the same.

It is also possible to specify a member function directly, as in:

         QSharedPointer<MyObject> obj =
             QSharedPointer<MyObject>(new MyObject, &QObject::deleteLater);

See also clear().

### template <typename Deleter> QSharedPointer::QSharedPointer(std::nullptr_t, Deleter d)

Creates a QSharedPointer that is null. This is equivalent to the QSharedPointer default constructor.

The deleter parameter d specifies the custom deleter for this object. The custom deleter is called, instead of the operator delete(), when the strong reference count drops to 0.

### 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.

### `[noexcept]` QSharedPointer::QSharedPointer(QSharedPointer<T> &&other)

Move-constructs a QSharedPointer instance, making it point at the same object that other was pointing to.

### 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.

### template <typename X> 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().

### `[since 6.9]` template <typename X> QSharedPointer<X> QSharedPointer::constCast() &&

The returned QSharedPointer shares ownership with the same set of shared owners as `*this`.

This function resets `*this` to `nullptr` on success.

This function overloads QSharedPointer::constCast().

This function was introduced in Qt 6.9.

See also isNull() and qSharedPointerConstCast().

### `[static]` template <typename... Args> QSharedPointer<T> QSharedPointer::create(Args &&... args)

Creates a QSharedPointer object and allocates a new item of type `T`. The QSharedPointer internals and the object are allocated in one single memory allocation, which could help reduce memory fragmentation in a long-running application.

This function will attempt to call a constructor for type `T` that can accept all the arguments passed (args). Arguments will be perfectly-forwarded.

### 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.

### template <typename X> 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().

### `[since 6.9]` template <typename X> QSharedPointer<X> QSharedPointer::dynamicCast() &&

The returned QSharedPointer shares ownership with the same set of shared owners as `*this`.

This function resets `*this` to `nullptr` on success.

This function overloads QSharedPointer::dynamicCast().

This function was introduced in Qt 6.9.

See also qSharedPointerDynamicCast().

### T *QSharedPointer::get() const

Same as data().

This function is provided for API compatibility with `std::shared_ptr`.

### bool QSharedPointer::isNull() const

Returns `true` if this object refers to `nullptr`.

### template <typename X> 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.

See also qSharedPointerObjectCast().

### `[since 6.9]` template <typename X> QSharedPointer<X> QSharedPointer::objectCast() &&

The returned QSharedPointer shares ownership with the same set of shared owners as `*this`.

This function resets `*this` to `nullptr` on success.

This function overloads QSharedPointer::objectCast().

This function was introduced in Qt 6.9.

See also qSharedPointerObjectCast().

### void QSharedPointer::reset()

Same as clear(). For std::shared_ptr compatibility.

### void QSharedPointer::reset(T *t)

Resets this QSharedPointer object to point to t instead. Equivalent to:

         QSharedPointer<T> other(t); this->swap(other);

### template <typename Deleter> void QSharedPointer::reset(T *t, Deleter deleter)

Resets this QSharedPointer object to point to t instead, with the Deleter deleter. Equivalent to:

         QSharedPointer<T> other(t, deleter); this->swap(other);

### template <typename X> 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().

### `[since 6.9]` template <typename X> QSharedPointer<X> QSharedPointer::staticCast() &&

The returned QSharedPointer shares ownership with the same set of shared owners as `*this`.

This function resets `*this` to `nullptr` on success.

This function overloads QSharedPointer::staticCast().

This function was introduced in Qt 6.9.

See also dynamicCast(), constCast(), and qSharedPointerCast().

### `[noexcept]` void QSharedPointer::swap(QSharedPointer<T> &other)

Swaps this shared pointer instance with other. This operation is very fast and never fails.

### QWeakPointer<T> QSharedPointer::toWeakRef() const

Returns a weak reference object that shares the pointer referenced by this object.

See also QWeakPointer::QWeakPointer().

### bool QSharedPointer::operator bool() const

Returns `true` if the contained pointer is not `nullptr`. This function is suitable for use in `if-constructs`, like:

         if (sharedptr) { ... }

See also isNull().

### bool QSharedPointer::operator!() const

Returns `true` if this object refers to `nullptr`. 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.

If the contained pointer is `nullptr`, behavior is undefined.

See also isNull().

### T *QSharedPointer::operator->() const

Provides access to the shared pointer's members.

If the contained pointer is `nullptr`, behavior is undefined.

See also isNull().

### `[noexcept]` QSharedPointer<T> &QSharedPointer::operator=(QSharedPointer<T> &&other)

Move-assigns other to this QSharedPointer instance.

### `[noexcept]` template <typename X> QSharedPointer<T> &QSharedPointer::operator=(QSharedPointer<X> &&other)

Move-assigns other to this QSharedPointer instance.

#### Constraints

Participates in overload resolution only if `X*` implicitly converts to `T*`.

### 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

### `[noexcept]` template <typename T> size_t qHash(const QSharedPointer<T> &key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

### template <typename X, typename T> 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().

### `[since 6.9]` template <typename X, typename T> QSharedPointer<X> qSharedPointerCast(QSharedPointer<T> &&other)

The returned QSharedPointer shares ownership with the same set of shared owners as other.

This function resets other to `nullptr` on success.

This function overloads QSharedPointer::qSharedPointerCast(const QSharedPointer<T> &other).

This function was introduced in Qt 6.9.

See also QSharedPointer::staticCast(), qSharedPointerDynamicCast(), and qSharedPointerConstCast().

### template <typename X, typename T> QSharedPointer<X> qSharedPointerCast(const QWeakPointer<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.

The other object is converted first to a strong reference. If that conversion fails (because the object it's pointing to has already been deleted), this function returns a null QSharedPointer.

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 QWeakPointer::toStrongRef(), qSharedPointerDynamicCast(), and qSharedPointerConstCast().

### template <typename X, typename T> QSharedPointer<X> qSharedPointerConstCast(const QSharedPointer<T> &src)

Returns a shared pointer to the pointer held by src, 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().

### `[since 6.9]` template <typename X, typename T> QSharedPointer<X> qSharedPointerConstCast(QSharedPointer<T> &&src)

The returned QSharedPointer shares ownership with the same set of shared owners as src.

This function resets src to `nullptr` on success.

This function overloads QSharedPointer::qSharedPointerConstCast(const QSharedPointer<T> &src).

This function was introduced in Qt 6.9.

See also QSharedPointer::constCast(), qSharedPointerCast(), and qSharedPointerDynamicCast().

### template <typename X, typename T> QSharedPointer<X> qSharedPointerConstCast(const QWeakPointer<T> &src)

Returns a shared pointer to the pointer held by src, 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.

The src object is converted first to a strong reference. If that conversion fails (because the object it's pointing to has already been deleted), this function returns a null QSharedPointer.

See also QWeakPointer::toStrongRef(), qSharedPointerCast(), and qSharedPointerDynamicCast().

### template <typename X, typename T> QSharedPointer<X> qSharedPointerDynamicCast(const QSharedPointer<T> &src)

Returns a shared pointer to the pointer held by src, 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().

### `[since 6.9]` template <typename X, typename T> QSharedPointer<X> qSharedPointerDynamicCast(QSharedPointer<T> &&src)

The returned QSharedPointer shares ownership with the same set of shared owners as src.

This function resets src to `nullptr` on success.

This function overloads QSharedPointer::qSharedPointerDynamicCast(const QSharedPointer<T> &src).

This function was introduced in Qt 6.9.

See also QSharedPointer::dynamicCast(), qSharedPointerCast(), and qSharedPointerConstCast().

### template <typename X, typename T> QSharedPointer<X> qSharedPointerDynamicCast(const QWeakPointer<T> &src)

Returns a shared pointer to the pointer held by src, 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.

The src object is converted first to a strong reference. If that conversion fails (because the object it's pointing to has already been deleted), this function also returns a null QSharedPointer.

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 QWeakPointer::toStrongRef(), qSharedPointerCast(), and qSharedPointerConstCast().

### template <typename X, typename T> QSharedPointer<X> qSharedPointerObjectCast(const QSharedPointer<T> &src)

The qSharedPointerObjectCast function is for casting a shared pointer.

Returns a shared pointer to the pointer held by src, 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.

See also QSharedPointer::objectCast(), qSharedPointerCast(), and qSharedPointerConstCast().

### `[since 6.9]` template <typename X, typename T> QSharedPointer<X> qSharedPointerObjectCast(QSharedPointer<T> &&src)

The returned QSharedPointer shares ownership with the same set of shared owners as src.

This function resets src to `nullptr` on success.

This function overloads QSharedPointer::qSharedPointerObjectCast(const QSharedPointer<T> &src).

This function was introduced in Qt 6.9.

See also QSharedPointer::objectCast(), qSharedPointerCast(), and qSharedPointerConstCast().

### template <typename X, typename T> QSharedPointer<X> qSharedPointerObjectCast(const QWeakPointer<T> &src)

The qSharedPointerObjectCast function is for casting a shared pointer.

Returns a shared pointer to the pointer held by src, 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.

The src object is converted first to a strong reference. If that conversion fails (because the object it's pointing to has already been deleted), this function also returns a null QSharedPointer.

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 QWeakPointer::toStrongRef(), qSharedPointerCast(), and qSharedPointerConstCast().

### template <typename X, typename T> std::shared_ptr<X> qSharedPointerObjectCast(const std::shared_ptr<T> &src)

Returns a shared pointer to the pointer held by src, 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 const_pointer_cast to cast away the constness.

### template <typename X, typename T> std::shared_ptr<X> qSharedPointerObjectCast(std::shared_ptr<T> &&src)

Returns a shared pointer to the pointer held by src, using a qobject_cast() to type `X` to obtain an internal pointer of the appropriate type.

If the `qobject_cast` succeeds, the function will return a valid shared pointer, and src is reset to null. If the `qobject_cast` fails, the object returned will be null, and src will not be modified.

Note that `X` must have the same cv-qualifiers (`const` and `volatile`) that `T` has, or the code will fail to compile. Use const_pointer_cast to cast away the constness.

### template <typename X, typename T> std::shared_ptr<X> qobject_pointer_cast(const std::shared_ptr<T> &src)

Returns a shared pointer to the pointer held by src.

Same as qSharedPointerObjectCast(). This function is provided for STL compatibility.

### template <typename X, typename T> std::shared_ptr<X> qobject_pointer_cast(std::shared_ptr<T> &&src)

Same as qSharedPointerObjectCast(). This function is provided for STL compatibility.

### template <typename T, typename X> bool operator!=(const QSharedPointer<T> &lhs, const QSharedPointer<X> &rhs)

Returns `true` if lhs and rhs refer to distinct pointers.

If rhs's template parameter is different from lhs's, QSharedPointer first needs to ensure that they are compatible types. It will attempt to perform an automatic `static_cast` to convert the types `T` and `X` to their composite pointer type. If rhs's template parameter is not a base or a derived type from lhs's, you will get a compiler error.

### template <typename T, typename X> bool operator!=(const QSharedPointer<T> &lhs, const X *rhs)

Returns `true` if lhs and rhs refer to distinct pointers.

If rhs's template parameter is different from lhs's, QSharedPointer first needs to ensure that they are compatible types. It will attempt to perform an automatic `static_cast` to convert the types `T` and `X` to their composite pointer type. If rhs's template parameter is not a base or a derived type from lhs's, you will get a compiler error.

### template <typename T> bool operator!=(const QSharedPointer<T> &lhs, std::nullptr_t)

Returns `true` if lhs refers to a valid (i.e. non-null) pointer.

See also QSharedPointer::isNull().

### template <typename T, typename X> bool operator!=(const T *lhs, const QSharedPointer<X> &rhs)

Returns `true` if the pointer lhs is not the same pointer as that referenced by rhs.

If rhs's template parameter is different from lhs's, QSharedPointer first needs to ensure that they are compatible types. It will attempt to perform an automatic `static_cast` to convert the types `T` and `X` to their composite pointer type. If rhs's template parameter is not a base or a derived type from lhs's, you will get a compiler error.

### template <typename T> bool operator!=(std::nullptr_t, const QSharedPointer<T> &rhs)

Returns `true` if rhs refers to a valid (i.e. non-null) pointer.

See also QSharedPointer::isNull().

### template <typename T> QDebug operator<<(QDebug debug, const QSharedPointer<T> &ptr)

Writes the pointer tracked by ptr into the debug object debug for debugging purposes.

See also Debugging Techniques.

### template <typename T, typename X> bool operator==(const QSharedPointer<T> &lhs, const QSharedPointer<X> &rhs)

Returns `true` if lhs and rhs refer to the same pointer.

If rhs's template parameter is different from lhs's, QSharedPointer first needs to ensure that they are compatible types. It will attempt to perform an automatic `static_cast` to convert the types `T` and `X` to their composite pointer type. If rhs's template parameter is not a base or a derived type from lhs's, you will get a compiler error.

### template <typename T, typename X> bool operator==(const QSharedPointer<T> &lhs, const X *rhs)

Returns `true` if lhs and rhs refer to the same pointer.

If rhs's template parameter is different from lhs's, QSharedPointer first needs to ensure that they are compatible types. It will attempt to perform an automatic `static_cast` to convert the types `T` and `X` to their composite pointer type. If rhs's template parameter is not a base or a derived type from lhs's, you will get a compiler error.

### template <typename T> bool operator==(const QSharedPointer<T> &lhs, std::nullptr_t)

Returns `true` if lhs refers to `nullptr`.

See also QSharedPointer::isNull().

### template <typename T, typename X> bool operator==(const T *lhs, const QSharedPointer<X> &rhs)

Returns `true` if the pointer lhs is the same pointer as that referenced by rhs.

If rhs's template parameter is different from lhs's, QSharedPointer first needs to ensure that they are compatible types. It will attempt to perform an automatic `static_cast` to convert the types `T` and `X` to their composite pointer type. If rhs's template parameter is not a base or a derived type from lhs's, you will get a compiler error.

### template <typename T> bool operator==(std::nullptr_t, const QSharedPointer<T> &rhs)

Returns `true` if rhs refers to `nullptr`.

See also QSharedPointer::isNull().