The QExplicitlySharedDataPointer class represents a pointer to an explicitly shared object. 更多...
| 頭: | #include <QExplicitlySharedDataPointer> |
| qmake: | QT += core |
| Since: | Qt 4.4 |
注意: 此類的所有函數 可重入 .
| typedef | Type |
| QExplicitlySharedDataPointer () | |
| QExplicitlySharedDataPointer (T * sharedData ) | |
| QExplicitlySharedDataPointer (const QExplicitlySharedDataPointer<T> & other ) | |
| QExplicitlySharedDataPointer (const QExplicitlySharedDataPointer<X> & other ) | |
| QExplicitlySharedDataPointer (QExplicitlySharedDataPointer && o ) | |
| ~QExplicitlySharedDataPointer () | |
| const T * | constData () const |
| T * | data () const |
| void | detach () |
| void | reset () |
| void | swap (QExplicitlySharedDataPointer & other ) |
| operator bool () const | |
| bool | operator! () const |
| bool | operator!= (const QExplicitlySharedDataPointer<T> & other ) const |
| bool | operator!= (const T * ptr ) const |
| T & | operator* () const |
| T * | operator-> () |
| T * | operator-> () const |
| QExplicitlySharedDataPointer<T> & | operator= (const QExplicitlySharedDataPointer<T> & other ) |
| QExplicitlySharedDataPointer & | operator= (T * sharedData ) |
| QExplicitlySharedDataPointer<T> & | operator= (QExplicitlySharedDataPointer<T> && other ) |
| bool | operator== (const QExplicitlySharedDataPointer<T> & other ) const |
| bool | operator== (const T * ptr ) const |
| T * | clone () |
The QExplicitlySharedDataPointer class represents a pointer to an explicitly shared object.
QExplicitlySharedDataPointer <T> makes writing your own explicitly shared classes easy. QExplicitlySharedDataPointer 實現 綫程安全 reference counting, ensuring that adding QExplicitlySharedDataPointers to your 可重入 classes won't make them non-reentrant.
Except for one big difference, QExplicitlySharedDataPointer is just like QSharedDataPointer . The big difference is that member functions of QExplicitlySharedDataPointer do not do the automatic copy on write operation ( detach ()) that non-const members of QSharedDataPointer do before allowing the shared data object to be modified. There is a detach () function available, but if you really want to detach (), you have to call it yourself. This means that QExplicitlySharedDataPointers behave like regular C++ pointers, except that by doing reference counting and not deleting the shared data object until the reference count is 0, they avoid the dangling pointer problem.
It is instructive to compare QExplicitlySharedDataPointer with QSharedDataPointer by way of an example. Consider the Employee example in QSharedDataPointer , modified to use explicit sharing as explained in the discussion 隱式 vs 明確共享 .
Note that if you use this class but find you are calling detach () a lot, you probably should be using QSharedDataPointer 代替。
In the member function documentation, d pointer always refers to the internal pointer to the shared data object.
另請參閱 QSharedData and QSharedDataPointer .
這是共享數據對象的類型。 d pointer 指嚮此類型的對象。
構造 QExplicitlySharedDataPointer initialized with a null d pointer .
構造 QExplicitlySharedDataPointer with d pointer 設為 sharedData 並遞增 sharedData 的引用計數。
This standard copy constructor sets the d pointer of this 到 d pointer in other and increments the reference count of the shared data object.
This copy constructor is different in that it allows other to be a different type of explicitly shared data pointer but one that has a compatible shared data object.
默認情況下,
d pointer
of
other
(of type
X *
) gets implicitly converted to the type
T *
; the result of this conversion is set as the
d pointer
of
this
, and the reference count of the shared data object is incremented.
However, if the macro
QT_ENABLE_QEXPLICITLYSHAREDDATAPOINTER_STATICCAST
is defined before including the
QExplicitlySharedDataPointer
header, then the
d pointer
of
other
undergoes a
static_cast
to the type
T *
. The result of the cast is then set as the
d pointer
of
this
, and the reference count of the shared data object is incremented.
警告:
relying on such
static_cast
is potentially dangerous, because it allows code like this to compile:
QExplicitlySharedDataPointer<Base> base(new Base); QExplicitlySharedDataPointer<Derived> derived(base); // !!! DANGER !!!
Starting from Qt 5.4 the cast is disabled by default. It is possible to enable it back by defining the
QT_ENABLE_QEXPLICITLYSHAREDDATAPOINTER_STATICCAST
macro, and therefore to allow old code (that relied on this feature) to compile without modifications.
移動構造 QExplicitlySharedDataPointer 實例,使之指嚮同一對象如 o 所指嚮的。
該函數在 Qt 5.2 引入。
Decrements the reference count of the shared data object. If the reference count becomes 0, the shared data object is deleted. This is then destroyed.
[protected]
T
*QExplicitlySharedDataPointer::
clone
()
Creates and returns a deep copy of the current data. This function is called by detach () when the reference count is greater than 1 in order to create the new copy. This function uses the operator new and calls the copy constructor of the type T.
見 QSharedDataPointer::clone () for an explanation of how to use it.
該函數在 Qt 4.5 引入。
返迴共享數據對象的 const 指針。
另請參閱 data ().
返迴共享數據對象指針。
If the shared data object's reference count is greater than 1, this function creates a deep copy of the shared data object and sets the d pointer of this to the copy.
因為 QExplicitlySharedDataPointer does not do the automatic copy on write operations that members of QSharedDataPointer do, detach() is not called automatically anywhere in the member functions of this class. If you find that you are calling detach() everywhere in your code, consider using QSharedDataPointer 代替。
重置 this to be null. i.e., this function sets the d pointer of this to 0, but first it decrements the reference count of the shared data object and deletes the shared data object if the reference count became 0.
Swap this instance's explicitly shared data pointer with the explicitly shared data pointer in other .
返迴
true
若
d pointer
of
this
is
not
null.
返迴
true
若
d pointer
of
this
為 null。
返迴
true
if
other
and
this
do
not
有相同
d pointer
.
返迴
true
若
d pointer
of
this
is
not
ptr
.
Provides access to the shared data object's members.
Provides access to the shared data object's members.
Provides const access to the shared data object's members.
設置 d pointer of this 到 d pointer of other and increments the reference count of the shared data object. The reference count of the old shared data object of this is decremented. If the reference count of the old shared data object becomes 0, the old shared data object is deleted.
設置 d pointer of this to sharedData 並遞增 sharedData 's reference count. The reference count of the old shared data object of this is decremented. If the reference count of the old shared data object becomes 0, the old shared data object is deleted.
移動賦值 other 到此 QExplicitlySharedDataPointer 實例。
該函數在 Qt 5.2 引入。
返迴
true
if
other
and
this
有相同
d pointer
.
返迴
true
若
d pointer
of
this
is
ptr
.