QSharedPointer 类

QSharedPointer class holds a strong reference to a shared pointer 更多...

头: #include <QSharedPointer>
qmake: QT += core
Since: Qt 4.5

注意: 此类的所有函数 可重入 .

公共函数

QSharedPointer ()
QSharedPointer (X * ptr )
QSharedPointer (X * ptr , Deleter deleter )
QSharedPointer ( std::nullptr_t )
QSharedPointer ( std::nullptr_t , Deleter d )
QSharedPointer (const QSharedPointer<T> & other )
QSharedPointer (const QWeakPointer<T> & other )
~QSharedPointer ()
void clear ()
QSharedPointer<X> constCast () const
T * data () const
QSharedPointer<X> dynamicCast () const
bool isNull () const
QSharedPointer<X> objectCast () const
void reset ()
void reset (T * t )
void reset (T * t , Deleter deleter )
QSharedPointer<X> staticCast () const
void swap (QSharedPointer<T> & other )
QWeakPointer<T> toWeakRef () const
operator bool () const
bool operator! () const
T & operator* () const
T * operator-> () const
QSharedPointer<T> & operator= (const QSharedPointer<T> & other )
QSharedPointer<T> & operator= (const QWeakPointer<T> & other )

静态公共成员

QSharedPointer<T> create ()
QSharedPointer<T> create ( ... )
QSharedPointer<X> qSharedPointerCast (const QSharedPointer<T> & other )
QSharedPointer<X> qSharedPointerConstCast (const QSharedPointer<T> & other )
QSharedPointer<X> qSharedPointerDynamicCast (const QSharedPointer<T> & other )
QSharedPointer<X> qSharedPointerObjectCast (const QSharedPointer<T> & other )
bool operator!= (const QSharedPointer<T> & ptr1 , const QSharedPointer<X> & ptr2 )
bool operator!= (const QSharedPointer<T> & ptr1 , const X * ptr2 )
bool operator!= (const T * ptr1 , const QSharedPointer<X> & ptr2 )
bool operator!= (const QSharedPointer<T> & lhs , std::nullptr_t )
bool operator!= ( std::nullptr_t , const QSharedPointer<T> & rhs )
QDebug operator<< (QDebug debug , const QSharedPointer<T> & ptr )
bool operator== (const QSharedPointer<T> & ptr1 , const QSharedPointer<X> & ptr2 )
bool operator== (const QSharedPointer<T> & ptr1 , const X * ptr2 )
bool operator== (const T * ptr1 , const QSharedPointer<X> & ptr2 )
bool operator== (const QSharedPointer<T> & lhs , std::nullptr_t )
bool operator== ( std::nullptr_t , const QSharedPointer<T> & rhs )

详细描述

QSharedPointer class holds a strong reference to a shared pointer

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.

线程安全

QSharedPointer and QWeakPointer are thread-safe and operate atomically on the pointer value. Different threads can also access the QSharedPointer or QWeakPointer pointing to the same 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.

其它指针类

Qt 还提供了 2 个其它指针包裹器类: 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.

最后, 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.

可选指针追踪

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 对象。

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.

另请参阅 QSharedDataPointer , QWeakPointer ,和 QScopedPointer .

成员函数文档编制

QSharedPointer:: QSharedPointer ()

创建 QSharedPointer that points to null (0).

QSharedPointer:: QSharedPointer ( X * ptr )

创建 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 ( X * ptr , Deleter deleter )

创建 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.

deleter parameter 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() 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);
					

另请参阅 clear ().

QSharedPointer:: QSharedPointer ( std::nullptr_t )

创建 QSharedPointer that is null. This is equivalent to the QSharedPointer default constructor.

该函数在 Qt 5.8 引入。

QSharedPointer:: QSharedPointer ( std::nullptr_t , Deleter d )

创建 QSharedPointer that is null. This is equivalent to the QSharedPointer default constructor.

该函数在 Qt 5.8 引入。

QSharedPointer:: QSharedPointer (const QSharedPointer < T > & other )

创建 QSharedPointer object that shares other 's pointer.

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 )

创建 QSharedPointer by promoting the weak reference other to strong reference and sharing its pointer.

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.

另请参阅 QWeakPointer::toStrongRef ().

QSharedPointer:: ~QSharedPointer ()

销毁此 QSharedPointer object. If it is the last reference to the pointer stored, this will delete the pointer as well.

void QSharedPointer:: clear ()

清零此 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 并返回 QSharedPointer that shares the reference. This function can be used for up- and for down-casting, but is more useful for up-casting.

另请参阅 isNull () 和 qSharedPointerConstCast ().

[static] QSharedPointer < T > QSharedPointer:: create ()

创建 QSharedPointer object and allocates a new item of type T 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 calls the default constructor for type T .

该函数在 Qt 5.1 引入。

[static] QSharedPointer < T > QSharedPointer:: create ( ... )

这是重载函数。

创建 QSharedPointer object and allocates a new item of type T 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. Arguments will be perfectly-forwarded.

注意: This function is only fully available with a C++11 compiler that supports perfect forwarding of an arbitrary number of arguments.

If the compiler does not support the necessary C++11 features, then a restricted version is available since Qt 5.4: you may pass one (but just one) argument, and it will always be passed by const reference.

If you target Qt before version 5.4, you must use the overload that calls the default constructor.

该函数在 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 QSharedPointer or QWeakPointer 对象。

QSharedPointer < X > QSharedPointer:: dynamicCast () const

Performs a dynamic cast from this pointer's type to X 并返回 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.

另请参阅 qSharedPointerDynamicCast ().

bool QSharedPointer:: isNull () const

返回 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 并返回 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.

该函数在 Qt 4.6 引入。

另请参阅 qSharedPointerObjectCast ().

void QSharedPointer:: reset ()

如同 clear (). For std::shared_ptr compatibility.

该函数在 Qt 5.0 引入。

void QSharedPointer:: reset ( T * t )

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

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

该函数在 Qt 5.0 引入。

void QSharedPointer:: reset ( T * t , Deleter deleter )

Resets this QSharedPointer object to point to t instead, with deleter deleter 。相当于:

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

该函数在 Qt 5.0 引入。

QSharedPointer < X > QSharedPointer:: staticCast () const

Performs a static cast from this pointer's type to X 并返回 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.

另请参阅 dynamicCast (), constCast (),和 qSharedPointerCast ().

void QSharedPointer:: swap ( QSharedPointer < T > & other )

Swaps this shared pointer instance with other 。此函数非常快,且从不失败。

该函数在 Qt 5.3 引入。

QWeakPointer < T > QSharedPointer:: toWeakRef () const

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

另请参阅 QWeakPointer::QWeakPointer ().

QSharedPointer:: operator bool () const

返回 true if this object is not null. This function is suitable for use in if-constructs , like:

if (sharedptr) { ... }
					

另请参阅 isNull ().

bool QSharedPointer:: operator! () const

返回 true if this object is null. This function is suitable for use in if-constructs , like:

if (!sharedptr) { ... }
					

另请参阅 isNull ().

T &QSharedPointer:: operator* () const

Provides access to the shared pointer's members.

另请参阅 isNull ().

T *QSharedPointer:: operator-> () const

Provides access to the shared pointer's members.

另请参阅 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.

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.

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

注意, 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.

另请参阅 QSharedPointer::staticCast (), qSharedPointerDynamicCast (),和 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 被忽略。

另请参阅 QSharedPointer::constCast (), qSharedPointerCast (),和 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.

注意, 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.

另请参阅 QSharedPointer::dynamicCast (), qSharedPointerCast (),和 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.

注意, 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.

该函数在 Qt 4.6 引入。

另请参阅 QSharedPointer::objectCast (), qSharedPointerCast (),和 qSharedPointerConstCast ().

bool operator!= (const QSharedPointer < T > & ptr1 , const QSharedPointer < X > & ptr2 )

返回 true if the pointer referenced by ptr1 is not the same pointer as that referenced by ptr2 .

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 )

返回 true if the pointer referenced by ptr1 is not the same pointer as ptr2 .

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 )

返回 true if the pointer ptr1 is not the same pointer as that referenced by ptr2 .

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 > & lhs , std::nullptr_t )

返回 true if the pointer referenced by lhs is a valid (i.e. non-null) pointer.

该函数在 Qt 5.8 引入。

另请参阅 QSharedPointer::isNull ().

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

返回 true if the pointer referenced by rhs is a valid (i.e. non-null) pointer.

该函数在 Qt 5.8 引入。

另请参阅 QSharedPointer::isNull ().

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

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

该函数在 Qt 5.7 引入。

另请参阅 调试技术 .

bool operator== (const QSharedPointer < T > & ptr1 , const QSharedPointer < X > & ptr2 )

返回 true if the pointer referenced by ptr1 is the same pointer as that referenced by ptr2 .

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 )

返回 true if the pointer referenced by ptr1 is the same pointer as ptr2 .

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 )

返回 true if the pointer ptr1 is the same pointer as that referenced by ptr2 .

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 > & lhs , std::nullptr_t )

返回 true if the pointer referenced by lhs is a null pointer.

该函数在 Qt 5.8 引入。

另请参阅 QSharedPointer::isNull ().

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

返回 true if the pointer referenced by rhs is a null pointer.

该函数在 Qt 5.8 引入。

另请参阅 QSharedPointer::isNull ().