当前位置: 首页 > article >正文

C++ QT中Q_Q和Q_D是什么?怎么使用?本质是什么?C++仿写

1.QT中Q_Q和Q_D是什么?

  • Q_Q可以得到外部可以访问的类指针
  • Q_D可以得到内部封装,外部不可达的类指针

2. 怎么使用?

上代码

  • APrivate.h
#pragma once
#include <QtCore>

class A;

class APrivate
{
	Q_DECLARE_PUBLIC(A)
public:
	APrivate();
	~APrivate();
public:
	void APrivateUseA();
private:
	friend A;
	A* q_ptr;
	int m_iA;
};
  • A.h
#pragma once
#include <QtCore>
class APrivate;

class A
{
	Q_DECLARE_PRIVATE(A)
public:
	A();
	~A();

public://对外接口
	void setValue(int& iValue);
	int getValue();

public:
	void APrivateCanUse();
	void APrivateUse();

private:
	APrivate* d_ptr;
};
  • A.cpp
#include "APrivate.h"
#include "A.h"
#include <iostream>

A::A():d_ptr(new APrivate) 
{
	d_ptr->q_ptr = this;
	std::cout << "A()" << std::endl;
}
A::~A() 
{
	Q_D(A);
	delete d;
	std::cout << "~A()" << std::endl;
}

void A::setValue(int& iValue)
{
	Q_D(A);
	d->m_iA = iValue;
}

int A::getValue()
{
	Q_D(A);
	return d->m_iA;
}

void A::APrivateCanUse()
{
	int iValue = 3;
	setValue(iValue);
}

void A::APrivateUse()
{
	Q_D(A);
	d->APrivateUseA();
}

APrivate::APrivate():m_iA(1)
{
	std::cout << "APrivate()" << std::endl;
}
APrivate::~APrivate() 
{
	std::cout << "~APrivate()" << std::endl;
}

void APrivate::APrivateUseA()
{
	Q_Q(A);
	q->APrivateCanUse();
}
  • main.cpp使用
#include <QtCore/QCoreApplication>
#include <iostream>
#include "A.h"
#include "APrivate.h"
#include <memory>

int main(int argc, char* argv[])
{
	QCoreApplication a(argc, argv);
	{	
		//1
		std::unique_ptr<A> Aa = std::make_unique<A>();
		std::cout << Aa->getValue() << std::endl;
		//2
		int Value = 2;
		Aa->setValue(Value);
		std::cout << Aa->getValue() << std::endl;
		//3
		Aa->APrivateUse();
		std::cout << Aa->getValue() << std::endl;

	}
	return a.exec();
}

输出:

APrivate()
A()
1
2
3
~APrivate()
~A()

3.本质是什么?

  • 详见QtCore文件夹下文的头文件qglobal.h
  • 摘录主要部分如下:
template <typename T> inline T *qGetPtrHelper(T *ptr) { return ptr; }
template <typename Ptr> inline auto qGetPtrHelper(Ptr &ptr) -> decltype(ptr.operator->()) { return ptr.operator->(); }

// The body must be a statement:
#define Q_CAST_IGNORE_ALIGN(body) QT_WARNING_PUSH QT_WARNING_DISABLE_GCC("-Wcast-align") body QT_WARNING_POP
#define Q_DECLARE_PRIVATE(Class) \
    inline Class##Private* d_func() \
    { Q_CAST_IGNORE_ALIGN(return reinterpret_cast<Class##Private *>(qGetPtrHelper(d_ptr));) } \
    inline const Class##Private* d_func() const \
    { Q_CAST_IGNORE_ALIGN(return reinterpret_cast<const Class##Private *>(qGetPtrHelper(d_ptr));) } \
    friend class Class##Private;

#define Q_DECLARE_PRIVATE_D(Dptr, Class) \
    inline Class##Private* d_func() \
    { Q_CAST_IGNORE_ALIGN(return reinterpret_cast<Class##Private *>(qGetPtrHelper(Dptr));) } \
    inline const Class##Private* d_func() const \
    { Q_CAST_IGNORE_ALIGN(return reinterpret_cast<const Class##Private *>(qGetPtrHelper(Dptr));) } \
    friend class Class##Private;

#define Q_DECLARE_PUBLIC(Class)                                    \
    inline Class* q_func() { return static_cast<Class *>(q_ptr); } \
    inline const Class* q_func() const { return static_cast<const Class *>(q_ptr); } \
    friend class Class;

#define Q_D(Class) Class##Private * const d = d_func()
#define Q_Q(Class) Class * const q = q_func()
  • 为了方便理解裁剪代码如下:(对照步骤2如何使用来理解)
template <typename T> inline T *qGetPtrHelper(T *ptr) { return ptr; }
//A.h中的声明,Class为A
#define Q_DECLARE_PRIVATE(Class) \
    inline Class##Private* d_func() \
    {return reinterpret_cast<Class##Private *>(qGetPtrHelper(d_ptr)); } \
    inline const Class##Private* d_func() const \
    {return reinterpret_cast<const Class##Private *>(qGetPtrHelper(d_ptr));} \
    friend class Class##Private;
//APrivate.h中的声明,Class为A
#define Q_DECLARE_PUBLIC(Class)                                    \
    inline Class* q_func() { return static_cast<Class *>(q_ptr); } \
    inline const Class* q_func() const { return static_cast<const Class *>(q_ptr); } \
    friend class Class;
//使用时的声明,取得临时指针
#define Q_D(Class) Class##Private * const d = d_func()
#define Q_Q(Class) Class * const q = q_func()

评论:

优:风格统一,使用便捷

劣:每次使用都要两个指针2*8=16个字节的额外开销

4.仿写

不用QT库,C++仿写

  • BPrivate.h
#pragma once
class B;

class BPrivate
{
public:
	BPrivate();
	~BPrivate();
public:
	void BPrivateUseB();
private:
	friend B;
	B* q_ptr;
	int m_iA;
};
  • B.h
#pragma once
class BPrivate;

class B
{
public:
	B();
	~B();

public://对外接口
	void setValue(int& iValue);
	int getValue();

public:
	void BPrivateCanUse();
	void BPrivateUse();

private:
	BPrivate* d_ptr;
};
  • B.cpp
#include "BPrivate.h"
#include "B.h"
#include <iostream>

B::B() :d_ptr(new BPrivate)
{
	d_ptr->q_ptr = this;
	std::cout << "B()" << std::endl;
}
B::~B()
{
	delete d_ptr;
	std::cout << "~B()" << std::endl;
}

void B::setValue(int& iValue)
{
	d_ptr->m_iA = iValue;
}

int B::getValue()
{
	return d_ptr->m_iA;
}

void B::BPrivateCanUse()
{
	int iValue = 3;
	setValue(iValue);
}

void B::BPrivateUse()
{
	d_ptr->BPrivateUseB();
}

BPrivate::BPrivate() :m_iA(1)
{
	std::cout << "BPrivate()" << std::endl;
}
BPrivate::~BPrivate()
{
	std::cout << "~BPrivate()" << std::endl;
}

void BPrivate::BPrivateUseB()
{
	q_ptr->BPrivateCanUse();
}
  • main.cpp
#include <QtCore/QCoreApplication>
#include <iostream>
#include "A.h"
#include "APrivate.h"
#include "B.h"
#include "BPrivate.h"
#include <memory>

int main(int argc, char* argv[])
{
	QCoreApplication a(argc, argv);
	{	
		//1
		std::unique_ptr<A> Aa = std::make_unique<A>();
		std::cout << Aa->getValue() << std::endl;
		//2
		int Value = 2;
		Aa->setValue(Value);
		std::cout << Aa->getValue() << std::endl;
		//3
		Aa->APrivateUse();
		std::cout << Aa->getValue() << std::endl;
	}

	{
		//1
		std::unique_ptr<B> Bb = std::make_unique<B>();
		std::cout << Bb->getValue() << std::endl;
		//2
		int Value = 2;
		Bb->setValue(Value);
		std::cout << Bb->getValue() << std::endl;
		//3
		Bb->BPrivateUse();
		std::cout << Bb->getValue() << std::endl;
	}
	return a.exec();
}
  • 输出(一样):
APrivate()
A()
1
2
3
~APrivate()
~A()
BPrivate()
B()
1
2
3
~BPrivate()
~B()


http://www.kler.cn/a/507888.html

相关文章:

  • git命令
  • 【Flink系列】6. Flink中的时间和窗口
  • 1.8 GPT-4:开创人工智能的新纪元
  • 【算法】枚举
  • 增广卡尔曼滤波AKF的要点分析
  • 【Rust自学】12.4. 重构 Pt.2:错误处理
  • ChatGPT的新任务调度功能是 2025 年 AI 的一个良好开端
  • GraphRAG如何使用ollama提供的llm model 和Embedding model服务构建本地知识库
  • 深度学习 Pytorch 张量(Tensor)的创建和常用方法
  • ChatGPT大模型极简应用开发-CH1-初识 GPT-4 和 ChatGPT
  • adb 常用命令总结
  • 小程序适配底部安全距离
  • 静态综合路由实验
  • b站视频(网页加客户端)+本地视频 生成回链
  • Git克隆报错
  • 【Element Plus系列】解决 DatePicker 组件清空时,触发两次change
  • STM32 FreeRTOS 事件标志组
  • 华为云Ubuntu中安装配置PostgreSQL与PostGIS
  • Hadoop 和 Spark 的内存管理机制分析
  • cuda 中BYTE*
  • 学习javaweb不懂得知识点或者细节
  • minio https配置
  • 【Docker】——安装Docker以及解决常见报错
  • 4、dockerfile实现lnmp
  • Python----Python高级(函数式编程,迭代器,生成器,装饰器)
  • Ettercap 入门