本页内容

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
继承自: QSharedDataPointerBase

注意:本类中的所有函数均为可重入函数。

QExplicitlySharedDataPointer 比较

类别可比较的类型
strongQExplicitlySharedDataPointer
strongT* 和 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()
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 ` 指定了从 `QSharedData` 派生的共享数据类的类型。

QExplicitlySharedDataPointer<T> 使编写您自己的显式共享类变得非常容易。QExplicitlySharedDataPointer 实现了线程安全的引用计数,确保将 QExplicitlySharedDataPointer 添加到您的可重入类中不会使其变得不可重入。

除了一个显著区别外,QExplicitlySharedDataPointer 与QSharedDataPointer 完全相同。该显著区别在于:QExplicitlySharedDataPointer 的成员函数不会执行自动“写时复制”操作(detach()),而QSharedDataPointer 的非 const 成员在允许修改共享数据对象之前会执行该操作。 虽然提供了detach()函数,但如果你真的想执行detach(),则必须自行调用该函数。这意味着QExplicitlySharedDataPointer的行为与普通的C++指针类似,只不过它会进行引用计数,并在引用计数为0之前不会删除共享数据对象,从而避免了悬空指针问题。

通过一个示例将 QExplicitlySharedDataPointer 与QSharedDataPointer 进行比较会很有启发。请参考QSharedDataPointer 中的Employee example ,该代码已根据Implicit vs Explicit Sharing 中的讨论进行修改,以使用显式共享。

请注意,如果您在使用该类时发现频繁调用detach(),那么您可能应该改用QSharedDataPointer 类。

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

另请参阅 QSharedData 和QSharedDataPointer 。

成员类型文档

QExplicitlySharedDataPointer::Type

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

成员函数文档

[noexcept] QExplicitlySharedDataPointer::QExplicitlySharedDataPointer()

构建一个 QExplicitlySharedDataPointer,其d 指针初始化为nullptr 。

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

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

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

该复制构造函数的特殊之处在于,它允许 `o ` 采用不同类型的显式共享数据指针,但该指针必须指向一个兼容的共享数据对象。

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

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

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

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

通过移动构造创建一个 QExplicitlySharedDataPointer 实例,使其指向与o 所指向的同一对象。

QExplicitlySharedDataPointer::~QExplicitlySharedDataPointer()

将共享数据对象的引用计数减1。如果引用计数变为0,则该共享数据对象将被删除。随后该对象将被销毁。

[protected] T *QExplicitlySharedDataPointer::clone()

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

有关使用方法的说明,请参阅QSharedDataPointer<T>::clone()。

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

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

另请参阅 data()。

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

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

void QExplicitlySharedDataPointer::detach()

如果共享数据对象的引用计数大于 1,则该函数会创建该共享数据对象的深度拷贝,并将this的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)

将该对象的d 指针设置为ptr ,并在ptr 不为nullptr 时,将ptr 的引用计数加 1。旧的共享数据对象的引用计数将减 1,若引用计数降至 0,则该对象将被删除。

该函数于 Qt 6.0 中引入。

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

将此显式共享的数据指针与other 互换。此操作速度极快,且绝不会失败。

[noexcept] T *QExplicitlySharedDataPointer::take()

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

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

[noexcept] QExplicitlySharedDataPointer::operator bool() const

如果该对象的d 指针 不为空,则返回true 。

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

如果该对象的d 指针为nullptr ,则返回true 。

T &QExplicitlySharedDataPointer::operator*() const

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

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

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

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

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

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

将other 通过Move操作赋值给此QExplicitlySharedDataPointer 实例。

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

将此对象的d 指针设置为o ,并递增o 的引用计数。此对象旧共享数据对象的引用计数将递减。如果旧共享数据对象的引用计数变为 0,则该旧共享数据对象将被删除。

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

将本对象的d 指针设置为o 的d 指针,并递增共享数据对象的引用计数。本对象原有共享数据对象的引用计数将递减。如果原有共享数据对象的引用计数变为 0,则该对象将被删除。

相关的非成员

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

如果lhs 和rhs 的d 指针 不相同,则返回true 。

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

如果rhs 的d指针 不是 lhs ,则返回true 。

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

如果 `lhs ` 和 `rhs ` 指向同一个`d` 指针,则返回 `true `。

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

如果 `rhs ` 的`d` 指针指向 `lhs`,则返回 `true `。

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