Qt文档阅读笔记-QScopedPointer解析及实例

Posted IT1995

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt文档阅读笔记-QScopedPointer解析及实例相关的知识,希望对你有一定的参考价值。

当指针超出范围后就会删除被引用的对象。 与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<MyClass> p(useSubClass ? new MyClass() : new MySubClass);
      QScopedPointer<QIODevice> device(handsOverOwnership());

      if (m_value > 3)
          return;

      process(device);
  

带限定符的C++指针使用QScopedPointer:

const QWidget *const p = new QWidget();
      // is equivalent to:
      const QScopedPointer<const QWidget> p(new QWidget());

      QWidget *const p = new QWidget();
      // is equivalent to:
      const QScopedPointer<QWidget> p(new QWidget());

      const QWidget *p = new QWidget();
      // is equivalent to:
      QScopedPointer<const QWidget> p(new QWidget());

自定义清空

相当于delete操作QScopedPointerDeleter

相当于delete []操作QScopedPointerArrayDeleter

相当于free操作QScopedPointerPodDeleter

相当于deleteLater操作QScopedPointerDeleteLater

代码如下:

// this QScopedPointer deletes its data using the delete[] operator:
  QScopedPointer<int, QScopedPointerArrayDeleter<int> > arrayPointer(new int[42]);

  // this QScopedPointer frees its data using free():
  QScopedPointer<int, QScopedPointerPodDeleter> podPointer(reinterpret_cast<int *>(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<MyCustomClass, ScopedPointerCustomDeleter> customPointer(new MyCustomClass);

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

class MyPrivateClass; // forward declare MyPrivateClass

  class MyClass
  
  private:
      QScopedPointer<MyPrivateClass> 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 <QString>
#include <QDebug>

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 <QDebug>

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 <QCoreApplication>
#include <QDebug>
#include <QScopedPointer>
#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<ClassA> p(new ClassA);


void usingQScopedCustomFunction()

    QScopedPointer<ClassA, ClassACustomDeleter> p(new ClassA);



int main(int argc, char *argv[])

    QCoreApplication a(argc, argv);

    normalFunction();
    qDebug() << "------------华丽的分割线------------";
    usingQScopedFunction();
    qDebug() << "------------华丽的分割线------------";
    usingQScopedCustomFunction();

    return a.exec();

 

以上是关于Qt文档阅读笔记-QScopedPointer解析及实例的主要内容,如果未能解决你的问题,请参考以下文章

Qt文档阅读笔记-Broadcast Sender Example解析

Qt文档阅读笔记-QtConcurrent Progress Dialog Example解析

Qt文档阅读笔记-QNetworkProxy::ProxyType解析(Qt设置Fiddler代理)

Qt文档阅读笔记-QNetworkProxy::ProxyType解析(Qt设置Fiddler代理)

Qt文档阅读笔记-staticMetaObject解析与实例

Qt文档阅读笔记-staticMetaObject解析与实例