Qt文档阅读笔记-QScopedPointer解析及实例
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 destructorQScopedPointer<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 pointerstruct ScopedPointerCustomDeleter{static inline void cleanup(MyCustomClass *pointer){myCustomDeallocator(pointer);}};// QScopedPointer using a custom deleter:QScopedPointer<MyCustomClass, ScopedPointerCustomDeleter> customPointer(new MyCustomClass);
下面是在类声明的时候要注意的问题:
class MyPrivateClass; // forward declare MyPrivateClassclass MyClass{private:QScopedPointer<MyPrivateClass> privatePtr; // QScopedPointer to forward declared classpublic:MyClass(); // OKinline ~MyClass() {} // VIOLATION - Destructor must not be inlineprivate: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//...//welldelete 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文档阅读笔记-Q_PROPERTY解析及实例
目录 官方解析 博主栗子 官方解析 这个宏用于继承于QObject的类声明属性.这样声明后的属性行为与类数据成员一样,但是他们可以通过元对象系统进行访问. Q_PROPERTY(type name(R ...
- Qt文档阅读笔记-QQmlApplicationEngine解析与实例(qml与C++混合编程及QQuick与widgets混合)
目录 官方解析 博主例子 官方解析 QQmlApplicationEngine提供了从一个QML文件里面加载应用程序的方式. 这类联合了QQmlEngine和QmlComponent去加载单独的QML ...
- Qt文档阅读笔记-Fortune Client Example实例解析
目录 官方解析 实例代码 博主增加解析 官方解析 Fortune Client Example 以使用QTcpSocket为例子,服务端可以配合Fortune Server或Threaded Fort ...
- Qt文档阅读笔记-Multiple Inheritance Example 实例解析及Automatic Connections解析
目录 Multiple Inheritance Example 实例解析 Automatic Connections解析 Multiple Inheritance Example 实例解析 这个实例很 ...
- Qt文档阅读笔记-QIODevice解析及Audio Example实例解析
目录 QIODevice官方解释及个人分析 Audio Example官方实例解析 QIODevice官方解释及个人分析 QIODevice类是Qt中I/O设备的接口. 提供了读和写的接口,QIODe ...
- Qt文档阅读笔记-Transformations解析及例子
目录 官方解析 博主小栗子 官方解析 QGraphicsItem除了基于他的坐标pos()外,还支持投影变化.下面提供了几种变化item的方式.下面来说明下简单的转换,可以通过调用setRotatio ...
- Qt文档阅读笔记-QLatin1String解析及文本段跨行赋值
QString的许多成员函数都被重载了,用于接收const char *.包括拷贝构造函数.分配符.操作符 insert().replace().indexOf().上述的函数都被优化避免const ...
- Qt文档阅读笔记-共享库的创建与调用
使用共享库的符号 这个符号可以作用在变量.类.函数中,并且这些都可以被调用端使用. 在编译共享库中,需要使用export符号.在使用端调用的时候使用import符号. 这里是本人从文档中记录的笔记,大 ...
- Qt文档阅读笔记-加载HeightMap(高度图)构造3D地形图
Qt文档阅读笔记-加载HeightMap(高度图)构造3D地形图 QHeightMapSurfaceDataProxy:是Q3DSurface的一个基本代理类. 他是专门加载高度图. 高度图是没有X, ...
最新文章
- android CMake开发
- JAVA知识总结目录
- [每日短篇] E - Base64 编码
- 动态规划求解0-1背包
- log4net 日志框架的配置
- Codeforces Round #330 (Div. 2) B. Pasha and Phone 容斥定理
- [TJOI2018]智力竞赛 (匈牙利)
- 【算法分析与设计】顺序存储结构的搜索算法
- SQL Server高级查询之T-SQL编程(存储过程)
- ssl charles 参数看不到_偷偷告诉你Charles的秘密
- 获取元素到页面上的位置
- python电影爬虫系统毕设_基于Python的豆瓣电影网络爬虫设计
- SecureCRT复制粘贴快捷设置
- JAVA基础篇(一)
- 学习制作FlappyBird时遇到的问题
- C语言课设实验设备管理系统(大作业)
- 《计算之魂》1.11.2--阅读心得
- 如何制作校园平面图及路线导图
- Anaconda安装报错(Failed to create Anaconda menus)
- J2SE:总结—基础很重要哈!