6.0.0-rc4Source

QSharedPointer Class

template <typename T> class QSharedPointer

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

Header#include <QSharedPointer>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes
Inheritsstd::shared_ptr

List of all members, including inherited members

Public Functions

QSharedPointer()
QSharedPointer(std::nullptr_t)
QSharedPointer(T *p)
QSharedPointer(T *p, Deleter d)
QSharedPointer(const Base &b)
QSharedPointer(const QSharedPointer<X> &o)
QSharedPointer(const QWeakPointer<T> &w)
T *data() const
boolisNull() const
voidclear()
QWeakPointer<T>toWeakRef() const
QSharedPointer<X>staticCast() const
QSharedPointer<X>dynamicCast() const
QSharedPointer<X>constCast() const
QSharedPointer<X>objectCast() const
voidswap(QSharedPointer &other)

Static Public Members

QSharedPointercreate(Args &&...args)

Detailed Description

QSharedPointer<T> shares ownership of an object through atomic reference counting. The object is deleted when the last QSharedPointer referring to it is destroyed or reset. QSharedPointer inherits from std::shared_ptr<T>, so the standard interface (get(), reset(), use_count(), operator*() and so on) is also available.

See also QWeakPointer and QEnableSharedFromThis.

Differences from Qt

QSharedPointer is a thin subclass of std::shared_ptr<T>, so reference counting and the deleter follow the standard library, and reset() is the inherited std::shared_ptr one. objectCast() uses qobject_cast, which in Nt is a dynamic_cast. There is no qSharedPointerFromVariant().

Member Function Documentation

QSharedPointer::QSharedPointer()

Constructs a null shared pointer.

QSharedPointer::QSharedPointer(std::nullptr_t)

Constructs a null shared pointer.

[explicit] QSharedPointer::QSharedPointer(T *p)

Constructs a shared pointer that takes ownership of p, which is deleted with delete when the last reference goes away.

template <typename Deleter>QSharedPointer::QSharedPointer(T *p, Deleter d)

This is an overloaded function.

Constructs a shared pointer that takes ownership of p and calls the deleter d to destroy it when the last reference goes away.

QSharedPointer::QSharedPointer(const Base &b)

This is an overloaded function.

Constructs a shared pointer from the std::shared_ptr b, sharing its ownership.

template <typename X, typename = std::enable_if_t<std::is_convertible_v<X *, T *>>>QSharedPointer::QSharedPointer(const QSharedPointer<X> &o)

This is an overloaded function.

Constructs a shared pointer from o, whose pointer type X * must be implicitly convertible to T *.

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

Constructs a shared pointer from the weak pointer w. The result is null if the object w referred to has already been destroyed.

See also QWeakPointer::toStrongRef().

void QSharedPointer::clear()

Releases this reference and makes the pointer null.

template <typename X>QSharedPointer<X> QSharedPointer::constCast() const

Returns a shared pointer to the same object converted to X * with const_cast. The reference count is shared with this pointer.

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

Creates a new T constructed from args and returns a shared pointer to it, allocating the object and its reference count together.

T * QSharedPointer::data() const

Returns the pointer to the shared object, or nullptr.

template <typename X>QSharedPointer<X> QSharedPointer::dynamicCast() const

Returns a shared pointer to the same object converted to X * with dynamic_cast. The result is null if the cast fails.

bool QSharedPointer::isNull() const

Returns true if this pointer is null; otherwise returns false.

template <typename X>QSharedPointer<X> QSharedPointer::objectCast() const

Returns a shared pointer to the same object converted to X * with qobject_cast. The result is null if the object is not an X. T and X must be QObject types.

template <typename X>QSharedPointer<X> QSharedPointer::staticCast() const

Returns a shared pointer to the same object converted to X * with static_cast. The reference count is shared with this pointer.

void QSharedPointer::swap(QSharedPointer &other)

Swaps this shared pointer with other.

QWeakPointer<T> QSharedPointer::toWeakRef() const

Returns a QWeakPointer that observes the same object without keeping it alive.

See also QWeakPointer::toStrongRef().