Qt文档阅读笔记-QScopedPointer解析及实例
> arrayPointer(new int[42]); // this QScopedPointer frees its data using free(): QScopedPointer podPointer(reinterpret_cast (malloc(42))); // this struct calls "myCustomDeallocator" to delete the pointer struct ScopedPointerCustomDeleter { static inline void cleanup(MyCustomClass *pointer) { myCustomDeallocator(pointer); } }; // QScopedPointer using a custom deleter: QScopedPointer customPointer(new MyCustomClass);
发布日期:2021-06-30 10:43:11
浏览次数:4
分类:技术文章
本文共 4251 字,大约阅读时间需要 14 分钟。
当指针超出范围后就会删除被引用的对象。
与QPointer不同,他可以在任意类型中使用(QPointer只能在identity type中使用)
4个不同的清除类
1. QScopedPointerDeleter
2. QScopedPointerArrayDeleter
3. QScopedPointerArrayDeleter
4. QScopedPointerDeleteLater
如下例子:QScopedPointer<int, QScopedPointerArrayDeleter<int>> intArrayPointer(new int(100));
在如下几个方面用得比较多:
异常
使代码简洁
通常用于删除动态分配的变量。
QPointerQScopedPointer基本上用用动态分配的对象,在范围外自动进行释放,这个范围是通过{}包起来的,也就是函数的生存周期。
如下面这个原始代码:
void myFunction(bool useSubClass) { MyClass *p = useSubClass ? new MyClass() : new MySubClass; QIODevice *device = handsOverOwnership(); if (m_value > 3) { delete p; delete device; return; } try { process(device); } catch (...) { delete p; delete device; throw; } delete p; delete device; }
使用了QScopedPointer后:
void myFunction(bool useSubClass) { // assuming that MyClass has a virtual destructor QScopedPointerp(useSubClass ? new MyClass() : new MySubClass); QScopedPointer device(handsOverOwnership()); if (m_value > 3) return; process(device); }
带限定符的C++指针使用QScopedPointer:
const QWidget *const p = new QWidget(); // is equivalent to: const QScopedPointerp(new QWidget()); QWidget *const p = new QWidget(); // is equivalent to: const QScopedPointer p(new QWidget()); const QWidget *p = new QWidget(); // is equivalent to: QScopedPointer p(new QWidget());
自定义清空
相当于delete操作QScopedPointerDeleter
相当于delete []操作QScopedPointerArrayDeleter
相当于free操作QScopedPointerPodDeleter
相当于deleteLater操作QScopedPointerDeleteLater
代码如下:
// this QScopedPointer deletes its data using the delete[] operator: QScopedPointer
下面是在类声明的时候要注意的问题:
class MyPrivateClass; // forward declare MyPrivateClass class MyClass { private: QScopedPointerprivatePtr; // QScopedPointer to forward declared class public: MyClass(); // OK inline ~MyClass() {} // VIOLATION - Destructor must not be inline private: Q_DISABLE_COPY(MyClass) // OK - copy constructor and assignment operators // are now disabled, so the compiler won't implicitely // generate them. };
运行截图如下:
代码如下:
#ifndef CLASSA_H#define CLASSA_H#include#include class ClassA;struct ClassACustomDeleter{ static inline void cleanup(ClassA *p){ qDebug() << "static inline void cleanup(ClassA *p) called"; delete p; }};class ClassA{public: ClassA(); ~ClassA(); const QString &getStr(); const int &getValue();private: QString m_str; int m_value;};#endif // CLASSA_H
ClassA.cpp
#include "ClassA.h"#includeClassA::ClassA() : m_str("字符串"), m_value(10){}ClassA::~ClassA(){ qDebug() << "ClassA::~ClassA() called";}const QString &ClassA::getStr(){ return m_str;}const int &ClassA::getValue(){ return m_value;}
main.cpp
#include#include #include #include "ClassA.h"void normalFunction(){ ClassA *a = new ClassA; try { if(0 == 0){ throw "error"; } } catch(char const *err) { qDebug() << err; delete a; return; } //want to do sth //... //well delete a;}void usingQScopedFunction(){ QScopedPointer p(new ClassA);}void usingQScopedCustomFunction(){ QScopedPointer p(new ClassA);}int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); normalFunction(); qDebug() << "------------华丽的分割线------------"; usingQScopedFunction(); qDebug() << "------------华丽的分割线------------"; usingQScopedCustomFunction(); return a.exec();}
转载地址:https://it1995.blog.csdn.net/article/details/106116187 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
不错!
[***.144.177.141]2024年04月30日 01时16分50秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
ping结果的相关知识点,tracert命令验证
2019-04-30
ARP欺骗原理与模拟
2019-04-30
网络运维相关知识点
2019-04-30
GNS3软件简介
2019-04-30
csdn搜索博主的文章
2019-04-30
ORA-12552: TNS:operation was interrupted
2019-04-30
MyISAM表修复
2019-04-30
GNS3中关联使用SecureCRT
2019-04-30
oracle日期的相关有用统计SQL
2019-04-30
oracle数据库LOB管理
2019-04-30
oracle数据库BFILE的应用demo
2019-04-30
Oracle数据库自定义类型--对象类型
2019-04-30
oracle分区详解
2019-04-30
SuperSlide.js
2019-04-30
oracle查看索引所在表空间及索引重建
2019-04-30
网络知识的三组数
2019-04-30
让nginx支持ipv6
2019-04-30
dbms_metadata.get_ddl的用法
2019-04-30
oracle中PL/SQL学习笔记
2019-04-30
telnet命令关闭tomcat
2019-04-30