gpt4 book ai didi

c++ - QSharedPointer 和 QSharedDataPointer 的区别?

转载 作者:IT老高 更新时间:2023-10-28 23:01:59 28 4
gpt4 key购买 nike

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

最佳答案

来自 Qt 文档 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.

示例用法-

 #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

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);

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


编辑

阅读时Memory management in Qt? , 我找到了这个链接 http://blog.qt.io/blog/2009/08/25/count-with-me-how-many-smart-pointer-classes-does-qt-have/ .对 Qt 的不同智能指针(当前 API 有 8 个)进行了非常出色的讨论。

关于c++ - QSharedPointer 和 QSharedDataPointer 的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3877514/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com