QExplicitlySharedDataPointer Class

template <typename T> class QExplicitlySharedDataPointer

QExplicitlySharedDataPointer 类表示指向显式共享对象的指针。更多

头文件: #include <QExplicitlySharedDataPointer>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

该类具有强可比性

该类与 T* 和 std::nullptr_t 具有强可比性

注意:该类中的所有函数都是可重入的

公共类型

公共函数

QExplicitlySharedDataPointer()
QExplicitlySharedDataPointer(T *data)
QExplicitlySharedDataPointer(const QExplicitlySharedDataPointer<X> &o)
QExplicitlySharedDataPointer(const QExplicitlySharedDataPointer<T> &o)
QExplicitlySharedDataPointer(QExplicitlySharedDataPointer<T> &&o)
~QExplicitlySharedDataPointer()
const T *constData() const
T *data() const
void detach()
(since 6.0) T *get() const
(since 6.0) void reset(T *ptr = nullptr)
void swap(QExplicitlySharedDataPointer<T> &other)
T *take()
bool operator bool() const
bool operator!() const
T &operator*() const
T *operator->()
T *operator->() const
QExplicitlySharedDataPointer<T> &operator=(QExplicitlySharedDataPointer<T> &&other)
QExplicitlySharedDataPointer<T> &operator=(T *o)
QExplicitlySharedDataPointer<T> &operator=(const QExplicitlySharedDataPointer<T> &o)

保护函数

T *clone()
bool operator!=(const QExplicitlySharedDataPointer<T> &lhs, const QExplicitlySharedDataPointer<T> &rhs)
bool operator!=(const T *const &lhs, const QExplicitlySharedDataPointer<T> &rhs)
bool operator==(const QExplicitlySharedDataPointer<T> &lhs, const QExplicitlySharedDataPointer<T> &rhs)
bool operator==(const T *const &lhs, const QExplicitlySharedDataPointer<T> &rhs)

详细说明

QExplicitlySharedDataPointer<T> 可让您轻松编写自己的显式共享类。QExplicitlySharedDataPointer 实现了线程安全的引用计数,确保在可重入类中添加 QExplicitlySharedDataPointers 不会使其成为不可重入类。

除了一个很大的不同,QExplicitlySharedDataPointer 与QSharedDataPointer 一样。最大的区别在于,QExplicitlySharedDataPointer 的成员函数不会QSharedDataPointer 的非const 成员那样,在允许修改共享数据对象之前进行自动复制写操作(detach()) 。虽然有detach() 函数可用,但如果您真的想要detach() ,就必须自己调用它。这意味着 QExplicitlySharedDataPointers 的行为与普通 C++ 指针类似,只是通过引用计数和在引用计数为 0 之前不删除共享数据对象,避免了悬空指针问题。

举例说明 QExplicitlySharedDataPointer 与QSharedDataPointer 的比较是很有启发性的。考虑QSharedDataPointer 中的Employee example ,根据讨论Implicit vs Explicit Sharing 中的解释,修改为使用显式共享。

请注意,如果您使用该类,但发现经常调用detach() ,那么您可能应该使用QSharedDataPointer 来代替。

在成员函数文档中,d 指针总是指共享数据对象的内部指针。

另请参见 QSharedDataQSharedDataPointer

成员类型文档

QExplicitlySharedDataPointer::Type

这是共享数据对象的类型。d 指针指向该类型的对象。

成员函数文档

[noexcept] QExplicitlySharedDataPointer::QExplicitlySharedDataPointer()

构造一个 QExplicitlySharedDataPointer,以nullptr 作为d 指针初始化。

[explicit noexcept] QExplicitlySharedDataPointer::QExplicitlySharedDataPointer(T *data)

构造一个 QExplicitlySharedDataPointer,其d 指针设置为data ,并增加data 的引用计数。

[noexcept] template <typename X> QExplicitlySharedDataPointer::QExplicitlySharedDataPointer(const QExplicitlySharedDataPointer<X> &o)

这个复制构造函数的不同之处在于,它允许o 成为不同类型的显式共享数据指针,但它拥有兼容的共享数据对象。

默认情况下,o (类型为X * )的d 指针会被隐式转换为T * 类型;转换的结果会被设置为this 的 d 指针,并且共享数据对象的引用计数会递增。

[noexcept] QExplicitlySharedDataPointer::QExplicitlySharedDataPointer(const QExplicitlySharedDataPointer<T> &o)

这个标准复制构造函数将thisd 指针设置为o 中的d 指针,并增加共享数据对象的引用计数。

[noexcept] QExplicitlySharedDataPointer::QExplicitlySharedDataPointer(QExplicitlySharedDataPointer<T> &&o)

Move- 构造一个 QExplicitlySharedDataPointer 实例,使其指向o 所指向的同一对象。

QExplicitlySharedDataPointer::~QExplicitlySharedDataPointer()

减少共享数据对象的引用计数。如果引用计数为 0,共享数据对象将被删除。然后将销毁。

[protected] T *QExplicitlySharedDataPointer::clone()

创建并返回当前数据的深度副本。当引用计数大于 1 时,detach() 会调用该函数来创建新副本。该函数使用操作符 new并调用 T 类型的副本构造函数。

有关如何使用该函数的解释,请参见QSharedDataPointer<T>::clone() 。

[noexcept] const T *QExplicitlySharedDataPointer::constData() const

返回指向共享数据对象的常量指针。

另请参见 data()。

[noexcept] T *QExplicitlySharedDataPointer::data() const

返回指向共享数据对象的指针。

void QExplicitlySharedDataPointer::detach()

如果共享数据对象的引用计数大于 1,该函数将创建共享数据对象的深度副本,并将其中d 指针设置为副本。

由于QExplicitlySharedDataPointer 不会像QSharedDataPointer 的成员那样在写操作自动进行复制,因此该类成员函数中的任何地方都不会自动调用 detach()。如果您发现代码中到处都在调用 detach(),请考虑使用QSharedDataPointer 代替。

[noexcept, since 6.0] T *QExplicitlySharedDataPointer::get() const

data() 相同。提供此函数是为了与 STL 兼容。

此函数在 Qt 6.0 中引入。

[noexcept, since 6.0] void QExplicitlySharedDataPointer::reset(T *ptr = nullptr)

thisd 指针设置为ptr ,如果ptr 不是nullptr ,则增加ptr 的引用计数。如果引用计数为 0,则递减旧共享数据对象的引用计数并删除该对象。

此函数在 Qt 6.0 中引入。

[noexcept] void QExplicitlySharedDataPointer::swap(QExplicitlySharedDataPointer<T> &other)

将这个显式共享数据指针与other 互换。这一操作速度非常快,而且从未出现过故障。

[noexcept] T *QExplicitlySharedDataPointer::take()

返回指向共享对象的指针,并将this重置为nullptr 。(也就是说,该函数将thisd 指针设置为nullptr 。)

注意: 返回对象的引用计数不会递减。该函数可与接收QAdoptSharedDataTag 标记对象的构造函数一起使用,以便在不进行原子操作的情况下传输共享数据对象。

[noexcept] bool QExplicitlySharedDataPointer::operator bool() const

如果thisd 指针 不为空,则返回true

[noexcept] bool QExplicitlySharedDataPointer::operator!() const

如果thisd 指针nullptr ,则返回true

T &QExplicitlySharedDataPointer::operator*() const

提供对共享数据对象成员的访问权限。

[noexcept] T *QExplicitlySharedDataPointer::operator->()

提供对共享数据对象成员的访问权限。

[noexcept] T *QExplicitlySharedDataPointer::operator->() const

提供对共享数据对象成员的常量访问。

[noexcept] QExplicitlySharedDataPointer<T> &QExplicitlySharedDataPointer::operator=(QExplicitlySharedDataPointer<T> &&other)

Move-assignsother 到此QExplicitlySharedDataPointer 实例。

[noexcept] QExplicitlySharedDataPointer<T> &QExplicitlySharedDataPointer::operator=(T *o)

thisd 指针设置为o ,并递增o 的引用计数。this的旧共享数据对象的引用计数会递减。如果旧共享数据对象的引用计数变为 0,则删除旧共享数据对象。

[noexcept] QExplicitlySharedDataPointer<T> &QExplicitlySharedDataPointer::operator=(const QExplicitlySharedDataPointer<T> &o)

thisd 指针设置为od 指针,并增加共享数据对象的引用计数。this的旧共享数据对象的引用计数会递减。如果旧共享数据对象的引用计数变为 0,则删除旧共享数据对象。

相关非成员

[noexcept] bool operator!=(const QExplicitlySharedDataPointer<T> &lhs, const QExplicitlySharedDataPointer<T> &rhs)

如果lhsrhsd 指针 一致,则返回true

[noexcept] bool operator!=(const T *const &lhs, const QExplicitlySharedDataPointer<T> &rhs)

如果rhsd 指针 不是 lhs ,则返回true

[noexcept] bool operator==(const QExplicitlySharedDataPointer<T> &lhs, const QExplicitlySharedDataPointer<T> &rhs)

如果lhsrhsd 指针相同,则返回true

[noexcept] bool operator==(const T *const &lhs, const QExplicitlySharedDataPointer<T> &rhs)

如果rhsd 指针lhs ,则返回true

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.