QPointer,QSharedPointer,QWeakPointer的区别与使用例子(QSharedPointer类似Delphi里的引用计数,是强引用,而QWeakPointer是弱引用,不影响原始对象的引用计数,相当于是在暗中观察对象,但保持联系,需要的时候就会出现)

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

46down voteaccepted:

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.

Consider this fragment:

QObject *obj = new QObject;
QPointer<QObject> pObj(obj);
delete obj;
Q_ASSERT(pObj.isNull()); // pObj will be nullptr now
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 there is a shared pointer, the object is not deleted!

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.

当对象被另一个单元所控制的时候,就可以使用QWeakPointer。

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() of isNull to determine that the pointer is null.

当使用QWeakPointer的时候,必须把它先转成QSharedPointer,你永远无法直接决定QWeakPointer是否是有效的。你只能使用data()去判断这个指针是否为空。

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.

一般来说,必须先转换成强引用。而强引用相当于锁定了这个对象。

https://stackoverflow.com/questions/22304118/what-is-the-difference-between-qpointer-qsharedpointer-and-qweakpointer-classes

QPointer,QSharedPointer,QWeakPointer的区别的更多相关文章

  1. QPointer,QSharedPointer,QWeakPointer的区别与使用例子(QSharedPointer类似Delphi里的引用计数,是强引用,而QWeakPointer是弱引用,不影响原始对象的引用计数,相当于是在暗中观察对象,但保持联系,需要的时候就会出现)

    QPointer is a template class that provides guarded pointers to Qt objects and behaves like a normal ...

  2. Qt智能指针QPointer, QSharedDataPointer ,QSharedPointer,QWeakPointer和QScopedPointer

    QPointer (4.0) 已经过时,可以被QWeakPointer所替代,它不是线程安全的. QSharedDataPointer (4.0) -- 提供对数据的COPY-ON-WRITE以及浅拷 ...

  3. 深入理解QStateMachine与QEventLoop事件循环的联系与区别

    最近一直在倒腾事件循环的东西,通过查看Qt源码多少还是有点心得体会,在这里记录下和大家分享.总之,对于QStateMachine状态机本身来说,需要有QEventLoop::exec()的驱动才能支持 ...

  4. QPointer很大程度上避免了野指针(使用if语句判断即可,类似于dynamic_cast),而且使用非常方便 good

    QPointer 如何翻译呢?我不太清楚,保留英文吧. The QPointer class is a template class that provides guarded pointers    ...

  5. c#与java的区别

    经常有人问这种问题,用了些时间java之后,发现这俩玩意除了一小部分壳子长的还有能稍微凑合上,基本上没什么相似之处,可以说也就是马甲层面上的相似吧,还是比较短的马甲... 一般C#多用于业务系统的开发 ...

  6. jquery和Js的区别和基础操作

    jqery的语法和js的语法一样,算是把js升级了一下,这两种语法可以一起使用,只不过是用jqery更加方便 一个页面想要使用jqery的话,先要引入一下jqery包,jqery包从网上下一个就可以, ...

  7. 【原】nodejs全局安装和本地安装的区别

    来微信支付有2年多了,从2年前的互联网模式转变为O2O模式,主要的场景是跟线下的商户去打交道,不像以往的互联网模式,有产品经理提需求,我们帮忙去解决问题. 转型后是这样的,团队成员更多需要去寻找业务的 ...

  8. 探究@property申明对象属性时copy与strong的区别

    一.问题来源 一直没有搞清楚NSString.NSArray.NSDictionary--属性描述关键字copy和strong的区别,看别人的项目中属性定义有的用copy,有的用strong.自己在开 ...

  9. X86和X86_64和X64有什么区别?

    x86是指intel的开发的一种32位指令集,从386开始时代开始的,一直沿用至今,是一种cisc指令集,所有intel早期的cpu,amd早期的cpu都支持这种指令集,ntel官方文档里面称为&qu ...

随机推荐

  1. 两个js冲突怎么解决?试试这四个方法

    两个js冲突很让前端头疼,虽然jquery是通用的,但调用不同经常会出问题.jQuery是目前流行的JS封装包,简化了很多复杂的JS程序,JQuery讲浏览器DOM树定义为$,通过$来获取各个子节点. ...

  2. Redis入门到高可用(三)——通用命令

    通用命令  参考 http://redisdoc.com/index.html 1. keys  #查看所有key 时间复杂度:O(N), N 为数据库中 key 的数量. 127.0.0.1:637 ...

  3. 关于Go,你可能不注意的7件事

    转自: https://tonybai.com/2015/09/17/7-things-you-may-not-pay-attation-to-in-go/ Go以简洁著称,但简洁中不乏值得玩味的小细 ...

  4. 什么是AOP面向切面编程

    什么是AOP 连接地址:http://blog.csdn.net/moreevan/article/details/11977115/ AOP(Aspect-OrientedProgramming,面 ...

  5. 强制SVN上传代码时添加日志

    因SVN不能像Git一样,审核代码之后才合入到主版本.为了使开发人员养成良好的上传习惯,强制要求上付时必须写明上传内容. 可以通过以下方法设置 1,在SVN服务器上找到源码仓库路径,找到下面文件夹:C ...

  6. 网站被植入Webshell的解决方案

    什么是Webshell 从字面上理解,”Web”指需要服务器开放Web服务,”shell”指取得对服务器的某种程度的操作权限.Webshell指匿名用户(入侵者)通过网站端口,获取网站服务器的一定操作 ...

  7. python 微信机器人,微信自动回复

    使用python现成的模块 itchat,可以实现,微信机器人的自动回复 其内部原理,是模拟了web版微信的登录,然后进行消息的接收发送,并不是只能用python实现,其他后端语言都可以做到 下面是使 ...

  8. jupyter 快捷键

    Jupyter Notebook 的快捷键 Jupyter Notebook 有两种键盘输入模式.编辑模式,允许你往单元中键入代码或文本:这时的单元框线是绿色的.命令模式,键盘输入运行程序命令:这时的 ...

  9. MongoDB--运行环境

    版本 a.b.c  a大版本  b中版本分奇数偶数 奇数是开发版 偶数是稳定版 c小版本 ssh工具:远程Linux机器.在windows下使用 Linux 下使用vim     windows下使用 ...

  10. C++编译变更stlport到使用g++的stl经验总结

    . 花了几天时间,需要把经验给记下来. 1. 需要支持C++11的编译器,最方便的方式就是 yum -y install devtoolset-6 在/usr/local/bin中增加gcc6.sh ...