Qt中的QPointer,QSharedPointer和QWeakPointer类之间有什么区别? [英] What is the difference between QPointer, QSharedPointer and QWeakPointer classes in Qt?

查看:1419
本文介绍了Qt中的QPointer,QSharedPointer和QWeakPointer类之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从Qt文档中阅读了有关QPointerQSharedPointerQWeakPointer类的信息.它说:

I have read from the Qt documentations about QPointer, QSharedPointer and QWeakPointer classes. It says:

  1. QPointer是一个模板类,它提供指向Qt对象的受保护的指针,并且其行为与普通的C ++指针类似,不同之处在于,在销毁引用的对象并且不生成任何悬挂指针"时,它会自动设置为0.

  1. QPointer is a template class that provides guarded pointers to Qt objects and behaves like a normal C++ pointer except that it is automatically set to 0 when the referenced object is destroyed and no "dangling pointers" are produced.

QSharedPointer类拥有对共享指针的强大引用.

QSharedPointer class holds a strong reference to a shared pointer.

QWeakPointer类持有对共享指针的弱引用.

QWeakPointer class holds a weak reference to a shared pointer.

我的问题是这些类之间有什么区别?".即,指向对象的指针和指向指针的引用之间有什么区别?它们都是指向具有不同机制和行为的对象的指针吗?

My questions is "What is the difference between these classes?". i.e what is the difference between a pointer to an object and a reference to a pointer? Are they all pointers to objects with different mechanisms and behaviors?

推荐答案

QPointer:
QPointer只能指向QObject实例.如果指向的对象被破坏,它将自动设置为nullptr.这是专门针对QObject的弱指针.

QPointer:
QPointer can only point to QObject instances. It will be automatically set to nullptr if the pointed to object is destroyed. It is a weak pointer specialized for QObject.

请考虑以下片段:

QObject *obj = new QObject;
QPointer<QObject> pObj(obj);
delete obj;
Q_ASSERT(pObj.isNull()); // pObj will be nullptr now

QSharedPointer
引用计数的指针.仅当销毁所有共享指针时,才会删除实际对象.等同于std::shared_ptr.

QSharedPointer
A reference-counted pointer. The actual object will only be deleted, when all shared pointers are destroyed. Equivalent to std::shared_ptr.

int *pI = new int;
QSharedPointer<int> pI1(pI);
QSharedPointer<int> pI2 = pI1;
pI1.clear();
// pI2 is still pointing to pI, so it is not deleted
pI2.clear();
// No shared pointers anymore, pI is deleted

请注意,只要有共享指针,就不会删除对象!

Note that as long as there is a shared pointer, the object is not deleted!

QWeakPointer:
可以保留对共享指针的弱引用.它不会阻止对象被破坏,只需重置即可.等同于std::weak_ptr,其中lock等同于toStrongRef.

QWeakPointer:
Can hold a weak reference to a shared pointer. It will not prevent the object from being destroyed, and is simply reset. Equivalent to std::weak_ptr, where lock is equivalent to toStrongRef.

int *pI = new int;
QSharedPointer<int> pI1(pI);
QWeakPointer<int> pI2 = pI1;
pI1.clear();
// No shared pointers anymore, pI is deleted
//
// To use the shared pointer, we must "lock" it for use:
QSharedPointer<int> pI2_locked = pI2.toStrongRef();
Q_ASSERT(pI2_locked.isNull());

如果您需要访问由另一个模块控制的对象,则可以使用它.

This can be used if you need access to an object that is controlled by another module.

要使用弱指针,必须将其转换为QSharedPointer.您应该永远不要基于弱指针有效的决定.您只能使用data()isNull()来确定指针为空.

To use a weak pointer, you must convert it to a QSharedPointer. You should never base a decision on the weak pointer being valid. You can only use data() or isNull() to determine that the pointer is null.

通常,要使用弱指针,必须将其转换为共享指针,因为这样的操作可确保对象在使用过程中一直存在.这等效于锁定"对象以进行访问,并且是使用弱指针指向的对象的唯一正确方法.

Generally, to use a weak pointer, you must convert it to a shared pointer since such an operation ensures that the object will survive for as long as you are using it. This is equivalent to "locking" the object for access and is the only correct way of using the object pointed to by a weak pointer.

QScopedPointer:
这只是一个助手类,当指针超出范围时,它将删除所引用的对象.因此,将动态分配的对象绑定到变​​量作用域.

QScopedPointer:
This is just a helper class that will delete the referenced object when the pointer goes out of scope. Thus, binds a dynamically allocated object to a variable scope.

您可以将其用于本地人的RAII语义,例如:

You can use this for RAII semantics for locals, e.g.:

MyClass *foo() {
    QScopedPointer<MyClass> myItem(new MyClass);
    // Some logic
    if (some condition) {
        return nullptr; // myItem will be deleted here
    }
    return myItem.take(); // Release item from scoped pointer and return it
}

如果有例外,该项目也会被删除

The item will also be deleted in case of an exception

另一个用例可以是对象的成员变量.然后,您不需要为这些编写析构函数:

Another use case can be member variables of an object. Then you don't need to write a destructor for those:

class MyClass {
public:
    MyClass() : myPtr(new int) {}
private:
    QScopedPointer<int> myPtr; // Will be deleted automatically when containing object is deleted
}

这篇关于Qt中的QPointer,QSharedPointer和QWeakPointer类之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆