QSharedPointer Class▲
-
Header: QSharedPointer
-
Since: Qt 4.5
-
qmake: QT += core
Detailed Description▲
The https:// is an automatic, shared pointer in C++. It behaves exactly like a normal pointer for normal purposes, including respect for constness.
https:// will delete the pointer it is holding when it goes out of scope, provided no other https:// objects are referencing it.
A https:// object can be created from a normal pointer, another https:// object or by promoting a https:// object to a strong reference.
Thread-Safety▲
https:// and https:// are reentrant classes. This means that, in general, a given https:// or https:// object cannot be accessed by multiple threads at the same time without synchronization.
Different https:// and https:// 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), https:// and https:// 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: https:// and https://. They are incompatible with one another, since each has its very different use case.
https:// 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 https:// and https://. The contents of the object pointed to by the pointer should not be considered shared, however: there is only one object. For that reason, https:// does not provide a way to detach or make copies of the pointed object.
https://, 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.
https:// is a variant of https://, except that it only detaches if https://() is explicitly called (hence the name).
https:// 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. https:// is lightweight, it makes no use of additional structure or reference counting.
Finally, https:// holds a pointer to a QObject-derived object, but it does so weakly. https:// has the same functionality, but its use for that function is deprecated.
Optional Pointer Tracking▲
A feature of https:// that can be enabled at compile-time for debugging purposes is a pointer tracking mechanism. When enabled, https:// registers in a global set all the pointers that it tracks. This allows one to catch mistakes like assigning the same pointer to two https:// objects.
This function is enabled by defining the QT_SHAREDPOINTER_TRACK_POINTERS macro before including the https:// header.
It is safe to use this feature even with code compiled without the feature. https:// 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, https://'s pointer tracking mechanism may fail to detect that the object being tracked is the same.
See Also▲
See also https://, https://, https://, https://
Member Function Documentation▲
QSharedPointer::QSharedPointer()▲
[explicit] QSharedPointer::QSharedPointer(X *ptr)▲
Since Qt 5.8, when the last reference to this https:// gets destroyed, ptr will be deleted by calling X's destructor (even if X is not the same as https://'s template parameter T). Previously, the destructor for T was called.
QSharedPointer::QSharedPointer(X *ptr, Deleter d)▲
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 https:// 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▲
See also https://()
[since 5.8] QSharedPointer::QSharedPointer(std::nullptr_t)▲
This function was introduced in Qt 5.8.
[since 5.8] QSharedPointer::QSharedPointer(std::nullptr_t, Deleter d)▲
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 function was introduced in Qt 5.8.
QSharedPointer::QSharedPointer(const QSharedPointer<T> &other)▲
If T is a derived type of the template parameter of this class, https:// will perform an automatic cast. Otherwise, you will get a compiler error.
[default] QSharedPointer::QSharedPointer(const QWeakPointer<T> &other)▲
If T is a derived type of the template parameter of this class, https:// will perform an automatic cast. Otherwise, you will get a compiler error.
See Also▲
See also https://()
QSharedPointer::~QSharedPointer()▲
void QSharedPointer::clear()▲
Clears this https:// 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 https:// that shares the reference. This function can be used for up- and for down-casting, but is more useful for up-casting.
See Also▲
See also https://(), https://()
[static, since 5.1] QSharedPointer<T> QSharedPointer::create(Args &&... args)▲
This is an overloaded function.
Creates a https:// object and allocates a new item of type T. The https:// 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.
This function was introduced in Qt 5.1.
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 https:// or https:// objects.
QSharedPointer<X> QSharedPointer::dynamicCast() const▲
Performs a dynamic cast from this pointer's type to X and returns a https:// that shares the reference. If this function is used to up-cast, then https:// will perform a dynamic_cast, which means that if the object being pointed by this https:// 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 https://() if you need to drop those qualifiers.
See Also▲
See also https://()
[since 5.11] T *QSharedPointer::get() const▲
Same as https://().
This function is provided for API compatibility with std::shared_ptr.
This function was introduced in Qt 5.11.
bool QSharedPointer::isNull() const▲
Returns true if this object refers to nullptr.
[since 4.6] QSharedPointer<X> QSharedPointer::objectCast() const▲
Performs a https://() from this pointer's type to X and returns a https:// that shares the reference. If this function is used to up-cast, then https:// will perform a qobject_cast, which means that if the object being pointed by this https:// 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 https://() if you need to drop those qualifiers.
This function was introduced in Qt 4.6.
See Also▲
See also https://()
[since 5.0] void QSharedPointer::reset()▲
Same as https://(). For std::shared_ptr compatibility.
This function was introduced in Qt 5.0.
[since 5.0] void QSharedPointer::reset(T *t)▲
Resets this https:// object to point to t instead. Equivalent to:
QSharedPointer<T> other(t); this->swap(other);This function was introduced in Qt 5.0.
[since 5.0] void QSharedPointer::reset(T *t, Deleter deleter)▲
Resets this https:// object to point to t instead, with the Deleter deleter. Equivalent to:
QSharedPointer<T> other(t, deleter); this->swap(other);This function was introduced in Qt 5.0.
QSharedPointer<X> QSharedPointer::staticCast() const▲
Performs a static cast from this pointer's type to X and returns a https:// 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 https://() if you need to drop those qualifiers.
See Also▲
See also https://(), https://(), https://()
[since 5.3] void QSharedPointer::swap(QSharedPointer<T> &other)▲
Swaps this shared pointer instance with other. This function is very fast and never fails.
This function was introduced in Qt 5.3.
int QSharedPointer::toWeakRef() const▲
Returns a weak reference object that shares the pointer referenced by this object.
See Also▲
See also https://()
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▲
See also https://()
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▲
See also https://()
T &QSharedPointer::operator*() const▲
Provides access to the shared pointer's members.
If the contained pointer is nullptr, behavior is undefined.
See Also▲
See also https://()
T *QSharedPointer::operator->() const▲
Provides access to the shared pointer's members.
If the contained pointer is nullptr, behavior is undefined.
See Also▲
See also https://()
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, https:// will perform an automatic cast. Otherwise, you will get a compiler error.
[default] int &QSharedPointer::operator=(const QWeakPointer<T> &)▲
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, https:// will perform an automatic cast. Otherwise, you will get a compiler error.
Related Non-Members▲
QSharedPointer<X> qSharedPointerCast(const QSharedPointer<T> &other)▲
Note that X must have the same cv-qualifiers (const and volatile) that T has, or the code will fail to compile. Use https:// to cast away the constness.
See Also▲
See also https://(), https://(), https://()
QSharedPointer<X> qSharedPointerCast(const QWeakPointer<T> &other)▲
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 https://.
Note that X must have the same cv-qualifiers (const and volatile) that T has, or the code will fail to compile. Use https:// to cast away the constness.
See Also▲
See also https://(), https://(), https://()
QSharedPointer<X> qSharedPointerConstCast(const QSharedPointer<T> &src)▲
See Also▲
See also https://(), https://(), https://()
QSharedPointer<X> qSharedPointerConstCast(const QWeakPointer<T> &src)▲
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 https://.
See Also▲
See also https://(), https://(), https://()
QSharedPointer<X> qSharedPointerDynamicCast(const QSharedPointer<T> &src)▲
Note that X must have the same cv-qualifiers (const and volatile) that T has, or the code will fail to compile. Use https:// to cast away the constness.
See Also▲
See also https://(), https://(), https://()
QSharedPointer<X> qSharedPointerDynamicCast(const QWeakPointer<T> &src)▲
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 https://.
Note that X must have the same cv-qualifiers (const and volatile) that T has, or the code will fail to compile. Use https:// to cast away the constness.
See Also▲
See also https://(), https://(), https://()
[since 4.6] QSharedPointer<X> qSharedPointerObjectCast(const QSharedPointer<T> &src)▲
Returns a shared pointer to the pointer held by src, using a https://() 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 https:// to cast away the constness.
This function was introduced in Qt 4.6.
See Also▲
See also https://(), https://(), https://()
[since 4.6] QSharedPointer<X> qSharedPointerObjectCast(const QWeakPointer<T> &src)▲
Returns a shared pointer to the pointer held by src, using a https://() 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 https://.
Note that X must have the same cv-qualifiers (const and volatile) that T has, or the code will fail to compile. Use https:// to cast away the constness.
This function was introduced in Qt 4.6.
See Also▲
See also https://(), https://(), https://()
bool operator!=(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2)▲
Returns true if ptr1 and ptr2 refer to distinct pointers.
If ptr2's template parameter is different from ptr1's, https:// 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 ptr1 and ptr2 refer to distinct pointers.
If ptr2's type is different from ptr1's, https:// 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, https:// 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.
[since 5.8] bool operator!=(const QSharedPointer<T> &lhs, std::nullptr_t)▲
Returns true if lhs refers to a valid (i.e. non-null) pointer.
This function was introduced in Qt 5.8.
See Also▲
See also https://()
[since 5.8] bool operator!=(std::nullptr_t, const QSharedPointer<T> &rhs)▲
Returns true if rhs refers to a valid (i.e. non-null) pointer.
This function was introduced in Qt 5.8.
See Also▲
See also https://()
bool operator==(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2)▲
Returns true if ptr1 and ptr2 refer to the same pointer.
If ptr2's template parameter is different from ptr1's, https:// 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 ptr1 and ptr2 refer to the same pointer.
If ptr2's type is different from ptr1's, https:// 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, https:// 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.
[since 5.8] bool operator==(const QSharedPointer<T> &lhs, std::nullptr_t)▲
Returns true if lhs refers to nullptr.
This function was introduced in Qt 5.8.
See Also▲
See also https://()
[since 5.8] bool operator==(std::nullptr_t, const QSharedPointer<T> &rhs)▲
Returns true if rhs refers to nullptr.
This function was introduced in Qt 5.8.
See Also▲
See also https://()


