当前位置:网站首页>QObject源码剖析-d指针和q指针
QObject源码剖析-d指针和q指针
2022-07-25 14:24:00 【vegetablesssss】
看看QObject源码:
class Q_CORE_EXPORT QObject
{
...
protected:
QScopedPointer<QObjectData> d_ptr;
...
}
可以看到QObject有一个protected成员d_ptr,其类型为QObjcetData,QScopedPointer类似于unique_ptr,看看QObjectData的源码:
class Q_CORE_EXPORT QObjectData {
public:
virtual ~QObjectData() = 0;
QObject *q_ptr;
....
};
上面的d_ptr和q_ptr就是d指针和q指针
看看QObject构造函数:
QObject::QObject(QObject *parent)
: d_ptr(new QObjectPrivate)
{
Q_D(QObject);
d_ptr->q_ptr = this;
....
}
可以看到,QObject在构造的时候给d_ptr赋值QObjectPrivate,看看源码:
class Q_CORE_EXPORT QObjectPrivate : public QObjectData
{
...
}
可以看到QObjectPrivate为QObjectData的子类,从QObject的构造函数可以看出,QObject和QObjectPrivate互相拥有对方的对象指针,那么QObject为什么要这么做呢?
实际上QObject的成员变量都放在QObjectPrivate中,为什么要将成员变量放在QObjectPrivate中然后在QObject中使用D指针d_ptr呢?实际上是为了解决编译和依赖问题。
- 编译问题:如果有20个cpp文件#include了qobject.h文件,那么一旦对这个.h文件的数据成员变量做那么一丁点修改,在下一次编译这个项目时就会导致这个10个cpp文件全部都必须重新编译。这也就是直接将类型的数据成员变量定义在类型本身的缺点。
- 其次是依赖问题。一般将dll提供给别人使用时,还必须包含相应的头文件,如果这时更改了QObject.h的成员,一个类型的数据成员变量修改之后,会导致类型的对象实例的内存布局发生变化,那么所有使用到该dll的地方都必须重新编译才能使用,而不能简单替换之前的dll就行。将数据成员使用指针保存,将该指针的实现放在cpp中就可以避免头文件改动,从而直接使用新的dll就行而不必使用到该dll的地方都重新编译。
其实q指针和d指针利用的编程技巧c++ PIMPL,有兴趣可以去查找相关资料。
边栏推荐
- Idea settings ignore file configuration when submitting SVN
- ~4.1 sword finger offer 05. replace spaces
- 机械制造业数字化新“引擎”供应链协同管理系统助力企业精细化管理迈上新台阶
- CDA level Ⅰ 2021 new version simulation question 2 (with answers)
- Realsense-Ros安装配置介绍与问题解决
- Vs2017 large factory ERP management system source code factory general ERP source code
- Depth estimation self-monitoring model monodepth2 paper summary and source code analysis [theoretical part]
- Typora无法打开提示安装新版本解决办法
- Emergency science | put away this summer safety guide and let children spend the summer vacation safely!
- jqgrid全选取消单行点击取消事件
猜你喜欢

filters获取data中的数据;filters使用data中的数据

Word set paste to retain only text

What you must know about data engineering in mlops
![Application practice: Great integrator of the paddy classification model [paddlehub, finetune, prompt]](/img/b6/62a346174bfa63fe352f9ef7596bfc.png)
Application practice: Great integrator of the paddy classification model [paddlehub, finetune, prompt]

基于浏览器的分屏阅读
![[directory blasting tool] information collection stage: robots.txt, Yujian, dirsearch, dirb, gobuster](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[directory blasting tool] information collection stage: robots.txt, Yujian, dirsearch, dirb, gobuster

Realsense-Ros安装配置介绍与问题解决

Realize a family security and environmental monitoring system (I)

Mongodb source code deployment and configuration

Comprehensive sorting and summary of maskrcnn code structure process of target detection and segmentation
随机推荐
PHP website design ideas
苹果手机端同步不成功,退出登录,结果再也登录不了了
基于PaddleOCR开发uni-app离线身份证识别插件
~4.1 sword finger offer 05. replace spaces
疫情之下,生物医药行业或将迎来突破性发展
Feiwo technology IPO meeting: annual revenue of 1.13 billion Hunan Cultural Tourism and Yuanli investment are shareholders
Initial flask and simple application
PT100 temperature measurement circuit diagram (AD590 typical temperature measurement circuit)
Nuc980 set up SSH xshell connection
手把手教你申请SSL证书
bond0脚本
基于redis的keys、scan删除ttl为-1的key
CDA level Ⅰ 2021 new version simulation question 1 (with answers)
Idea settings ignore file configuration when submitting SVN
Gateway 网关报错 SERVICE_UNAVAILABLE
实现一个家庭安防与环境监测系统(二)
Gartner 2022 top technology trend: Super automation
[directory blasting tool] information collection stage: robots.txt, Yujian, dirsearch, dirb, gobuster
Emergency science | put away this summer safety guide and let children spend the summer vacation safely!
Opencv video tracking "suggestions collection"