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
最新文章
- 自定义Inspector检视面板
- oracle 单列索引 多列索引的性能测试
- IE6/7/8中parseInt第一个参数为非法八进制字符串且第二个参数不传时返回值为0
- 发布HTML5 2D游戏引擎YEngine2D
- 一个关于echo的小知识点
- linux系统中删除文件夹
- Quartz Enterprise Job Scheduler 1.x Tutorial---转载
- js类型判断及鸭式辨型
- virtualBox 安装CentOS 全屏
- Android对px和dip进行尺寸转换的方法
- 3des用法示例,已测试
- MYSQL的空间查询
- mysql 索引原理
- 11-20 bom 浏览器对象模型
- c#随机生成英文名
- 学习windows编程 day3 之 自定义画笔的两种方法
- CentOS下如何查看并杀死僵尸进程
- Ubuntu下安装pycharm并设置快捷方式
- [转]Windows 下 Apache Virtual hosts 简单配置
- Dictionary GetOrAdd
热门文章
- cmder显示UTF-8字体
- Linux上安装JDK 分类: B1_JAVA B3_LINUX 2014-08-29 15:12 449人阅读 评论(0) 收藏
- [转载]netcore 使用surging框架发布到docker
- 【9207&&b701】统计数字(NOIP2007)
- Centos6.5 VNC 配置
- <;Linux>; Linux下一些常用命令(个人记录)
- 无法为数据库中的对象分配空间,因为&#39;PRIMARY&#39;文件组已满
- Codeforces Round #313 (Div. 2) 560C Gerald&;#39;s Hexagon(脑洞)
- 【t034】Matrix67的派对
- SpringMVC中支持多视图解析