QSharedPointer 和 QSharedDataPointer 之间的区别?

发布于 2024-09-26 21:21:26 字数 77 浏览 0 评论 0原文

这两种类型的指针有什么区别?据我所知,QSharedPointer可以很好地处理这种情况,那么QSharedDataPointer需要什么?

What is the difference between these two types of pointers? As far as I can read, QSharedPointer can handle situation well, so what is the need for QSharedDataPointer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沙沙粒小 2024-10-03 21:21:26

来自 Qt 文档
QSharedDataPointer

QSharedDataPointer 类
表示隐式指向的指针
共享对象。 Q共享数据指针
隐式地编写你自己的内容
共享课程很容易。
QSharedDataPointer 实现
线程安全的引用计数,
确保添加
指向您的可重入者的 QSharedDataPointers
类不会使它们不可重入。
许多Qt都使用隐式共享
课程结合了速度和
指针的内存效率
类的易用性。查看共享
课程页面了解更多信息。

用法示例 -

 #include <QSharedData>
 #include <QString>

 class EmployeeData : public QSharedData
 {
   public:
     EmployeeData() : id(-1) { }
     EmployeeData(const EmployeeData &other)
         : QSharedData(other), id(other.id), name(other.name) { }
     ~EmployeeData() { }

对于 QSharedPointer

QSharedPointer 类拥有一个
对共享指针的强引用
QSharedPointer 是一个自动的、
C++ 中的共享指针。它的行为
与普通指针完全相同
正常目的,包括尊重
恒定性。 QSharedPointer将删除
它移动时所持有的指针
超出范围,前提是没有其他
QSharedPointer 对象正在引用
它。

>  QSharedPointer<MyObject> obj =
>          QSharedPointer<MyObject>(new MyObject);

因此,QSharedDataPointer 用于创建隐式共享类。而 QSharedPointer 是一个指向类的引用计数智能指针。


编辑

在阅读Qt中的内存管理?时,我找到了此链接http://blog.qt.io/blog/2009/08/25/count-with-me-how-many-smart-pointer-classes-does-qt-have/。关于 Qt 不同智能指针的非常精彩的讨论(当前 API 有 8 个)。

From Qt documentation
QSharedDataPointer

The QSharedDataPointer class
represents a pointer to an implicitly
shared object. QSharedDataPointer
makes writing your own implicitly
shared classes easy.
QSharedDataPointer implements
thread-safe reference counting,
ensuring that adding
QSharedDataPointers to your reentrant
classes won't make them non-reentrant.
Implicit sharing is used by many Qt
classes to combine the speed and
memory efficiency of pointers with the
ease of use of classes. See the Shared
Classes page for more information.

Example usage -

 #include <QSharedData>
 #include <QString>

 class EmployeeData : public QSharedData
 {
   public:
     EmployeeData() : id(-1) { }
     EmployeeData(const EmployeeData &other)
         : QSharedData(other), id(other.id), name(other.name) { }
     ~EmployeeData() { }

For QSharedPointer

The QSharedPointer class holds a
strong reference to a shared pointer
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.

>  QSharedPointer<MyObject> obj =
>          QSharedPointer<MyObject>(new MyObject);

So, the QSharedDataPointer is used to make creating implicititly shared classes. Whereas QSharedPointer is a reference counting Smart pointer that points to classes.


EDIT

When reading Memory management in Qt?, I found this link http://blog.qt.io/blog/2009/08/25/count-with-me-how-many-smart-pointer-classes-does-qt-have/. A really excellent discussion of the different smart pointers Qt has (current API has 8).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文