Qt文档阅读笔记-QScopedPointer解析及实例
发布日期:2021-06-30 10:43:11 浏览次数:3 分类:技术文章

本文共 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      QScopedPointer
p(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 QScopedPointer
p(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
> 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);

下面是在类声明的时候要注意的问题:

class MyPrivateClass; // forward declare MyPrivateClass  class MyClass  {  private:      QScopedPointer
privatePtr; // 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"#include 
ClassA::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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:MySQL笔记-MySQL5.7解决root创建用户出现ERROR 1044(42000): Access denied for user
下一篇:Qt文档阅读笔记-QPointer的概念及实例(并发多线程实例)

发表评论

最新留言

不错!
[***.144.177.141]2024年04月18日 05时46分15秒