QSharedPointer 類保持共享指針強引用。 更多...
| 頭: | #include <QSharedPointer> |
| qmake: | QT += core |
| Since: | Qt 4.5 |
該類在 Qt 4.5 引入。
注意: 此類的所有函數 可重入 .
| QSharedPointer (QSharedPointer<X> && other ) | |
| QSharedPointer (const QWeakPointer<T> & other ) | |
| QSharedPointer (const QSharedPointer<T> & other ) | |
| QSharedPointer ( std::nullptr_t , Deleter d ) | |
| QSharedPointer ( std::nullptr_t ) | |
| QSharedPointer (X * ptr , Deleter d ) | |
| QSharedPointer (X * ptr ) | |
| QSharedPointer () | |
| QSharedPointer<T> & | operator= (const QWeakPointer<T> & other ) |
| QSharedPointer<T> & | operator= (const QSharedPointer<T> & other ) |
| ~QSharedPointer () | |
| void | clear () |
| QSharedPointer<X> | constCast () const |
| T * | data () const |
| QSharedPointer<X> | dynamicCast () const |
| T * | get () 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 |
| bool | operator bool () const |
| bool | operator! () const |
| T & | operator* () const |
| T * | operator-> () const |
| QSharedPointer<T> & | operator= (QSharedPointer<X> && other ) |
| QSharedPointer<T> | create (Args &&... args ) |
| size_t | qHash (const QSharedPointer<T> & key , size_t seed = 0) |
| QSharedPointer<X> | qSharedPointerCast (const QSharedPointer<T> & other ) |
| QSharedPointer<X> | qSharedPointerCast (const QWeakPointer<T> & other ) |
| QSharedPointer<X> | qSharedPointerConstCast (const QSharedPointer<T> & src ) |
| QSharedPointer<X> | qSharedPointerConstCast (const QWeakPointer<T> & src ) |
| QSharedPointer<X> | qSharedPointerDynamicCast (const QSharedPointer<T> & src ) |
| QSharedPointer<X> | qSharedPointerDynamicCast (const QWeakPointer<T> & src ) |
| QSharedPointer<X> | qSharedPointerObjectCast (const 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> & ptr1 , const QSharedPointer<X> & ptr2 ) |
| bool | operator!= (const QSharedPointer<T> & lhs , std::nullptr_t ) |
| bool | operator!= (const QSharedPointer<T> & ptr1 , const X * ptr2 ) |
| bool | operator!= ( std::nullptr_t , const QSharedPointer<T> & rhs ) |
| bool | operator!= (const T * ptr1 , const QSharedPointer<X> & ptr2 ) |
| QDebug | operator<< (QDebug debug , const QSharedPointer<T> & ptr ) |
| bool | operator== (const QSharedPointer<T> & ptr1 , const QSharedPointer<X> & ptr2 ) |
| bool | operator== (const QSharedPointer<T> & lhs , std::nullptr_t ) |
| bool | operator== (const QSharedPointer<T> & ptr1 , const X * ptr2 ) |
| bool | operator== ( std::nullptr_t , const QSharedPointer<T> & rhs ) |
| bool | operator== (const T * ptr1 , const QSharedPointer<X> & ptr2 ) |
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.
QSharedPointer 和 QWeakPointer 是可重入類。這意味著,一般而言,給定 QSharedPointer 或 QWeakPointer 對象 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.
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 是變體的 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 保持指針指嚮 QObject 派生對象,但它錶現得如此弱。 QWeakPointer 擁有相同功能,但棄用使用這種功能。
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.
另請參閱 QSharedDataPointer , QWeakPointer , QScopedPointer ,和 QEnableSharedFromThis .
Move-constructs a QSharedPointer instance, making it point at the same object that other 所指嚮的。
This constructor participates in overload resolution only if
X*
隱式轉換成
T*
.
該函數在 Qt 5.6 引入。
Creates a 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 對象共享 other 的指針。
若
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.
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.
該函數在 Qt 5.8 引入。
Creates a QSharedPointer that is null. This is equivalent to the QSharedPointer default constructor.
該函數在 Qt 5.8 引入。
創建 QSharedPointer 指嚮 ptr 。指針 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() 在 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 指嚮 ptr 。指針 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
被調用。
創建為 null 的 QSharedPointer (對象保持引用
nullptr
).
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.
使此對象共享 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 object. If it is the last reference to the pointer stored, this will delete the pointer as well.
清零此 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.
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]
template <typename Args>
QSharedPointer
<
T
> QSharedPointer::
create
(
Args
&&...
args
)
這是重載函數。
創建
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 (
args
). Arguments will be perfectly-forwarded.
該函數在 Qt 5.1 引入。
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 對象。
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 ().
如同 data ().
This function is provided for API compatibility with
std::shared_ptr
.
該函數在 Qt 5.11 引入。
返迴
true
若此對象引用
nullptr
.
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 ().
如同 clear (). For std::shared_ptr compatibility.
該函數在 Qt 5.0 引入。
Resets this QSharedPointer object to point to t instead. Equivalent to:
QSharedPointer<T> other(t); this->swap(other);
該函數在 Qt 5.0 引入。
Resets this QSharedPointer object to point to t instead, with the Deleter deleter 。相當於:
QSharedPointer<T> other(t, deleter); this->swap(other);
該函數在 Qt 5.0 引入。
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 ().
Swaps this shared pointer instance with other 。此函數非常快且從不失敗。
該函數在 Qt 5.3 引入。
Returns a weak reference object that shares the pointer referenced by this object.
另請參閱 QWeakPointer::QWeakPointer ().
返迴
true
if the contained pointer is not
nullptr
. This function is suitable for use in
if-constructs
, like:
if (sharedptr) { ... }
另請參閱 isNull ().
返迴
true
若此對象引用
nullptr
. This function is suitable for use in
if-constructs
, like:
if (!sharedptr) { ... }
另請參閱 isNull ().
Provides access to the shared pointer's members.
If the contained pointer is
nullptr
, behavior is undefined.
另請參閱 isNull ().
Provides access to the shared pointer's members.
If the contained pointer is
nullptr
, behavior is undefined.
另請參閱 isNull ().
移動賦值 other 到此 QSharedPointer 實例。
This assignment operator participates in overload resolution only if
X*
隱式轉換成
T*
.
該函數在 Qt 5.6 引入。
返迴哈希值為 key ,使用 seed 做計算種子。
該函數在 Qt 5.0 引入。
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 ().
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 .
注意,
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.
另請參閱 QWeakPointer::toStrongRef (), qSharedPointerDynamicCast (),和 qSharedPointerConstCast ().
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
被忽略。
另請參閱 QSharedPointer::constCast (), qSharedPointerCast (),和 qSharedPointerDynamicCast ().
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
被忽略。
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 .
另請參閱 QWeakPointer::toStrongRef (), qSharedPointerCast (),和 qSharedPointerDynamicCast ().
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.
注意,
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 ().
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 .
注意,
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.
另請參閱 QWeakPointer::toStrongRef (), qSharedPointerCast (),和 qSharedPointerConstCast ().
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.
注意,
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 ().
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 .
注意,
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 引入。
另請參閱 QWeakPointer::toStrongRef (), qSharedPointerCast (),和 qSharedPointerConstCast ().
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.
注意,
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.
該函數在 Qt 5.14 引入。
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.
若
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.
注意,
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.
該函數在 Qt 5.14 引入。
Returns a shared pointer to the pointer held by src .
如同 qSharedPointerObjectCast ()。此函數為兼容 STL (標準模闆庫) 提供。
該函數在 Qt 5.14 引入。
如同 qSharedPointerObjectCast ()。此函數為兼容 STL (標準模闆庫) 提供。
該函數在 Qt 5.14 引入。
返迴
true
if
ptr1
and
ptr2
refer to distinct pointers.
若
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.
返迴
true
if
lhs
refers to a valid (i.e. non-null) pointer.
該函數在 Qt 5.8 引入。
另請參閱 QSharedPointer::isNull ().
返迴
true
if
ptr1
and
ptr2
refer to distinct pointers.
若
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.
返迴
true
if
rhs
refers to a valid (i.e. non-null) pointer.
該函數在 Qt 5.8 引入。
另請參閱 QSharedPointer::isNull ().
返迴
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.
Writes the pointer tracked by ptr 到 debug 對象 debug 為調試目的。
該函數在 Qt 5.7 引入。
另請參閱 調試技術 .
返迴
true
if
ptr1
and
ptr2
refer to the same pointer.
若
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.
返迴
true
if
lhs
refers to
nullptr
.
該函數在 Qt 5.8 引入。
另請參閱 QSharedPointer::isNull ().
返迴
true
if
ptr1
and
ptr2
refer to the same pointer.
若
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.
返迴
true
if
rhs
refers to
nullptr
.
該函數在 Qt 5.8 引入。
另請參閱 QSharedPointer::isNull ().
返迴
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.