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

1.QT中Q_Q和Q_D是什么?

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

2. 怎么使用?

上代码

  • APrivate.h
cpp 复制代码
#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
cpp 复制代码
#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
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使用
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();
}

输出:

cpp 复制代码
APrivate()
A()
1
2
3
~APrivate()
~A()

3.本质是什么?

  • 详见QtCore文件夹下文的头文件qglobal.h
  • 摘录主要部分如下:
cpp 复制代码
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如何使用来理解**)**
cpp 复制代码
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
cpp 复制代码
#pragma once
class B;

class BPrivate
{
public:
	BPrivate();
	~BPrivate();
public:
	void BPrivateUseB();
private:
	friend B;
	B* q_ptr;
	int m_iA;
};
  • B.h
cpp 复制代码
#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
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
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();
}
  • 输出(一样):
cpp 复制代码
APrivate()
A()
1
2
3
~APrivate()
~A()
BPrivate()
B()
1
2
3
~BPrivate()
~B()
相关推荐
辞旧 lekkk5 小时前
【Qt】信号和槽
linux·开发语言·数据库·qt·学习·mysql·萌新
2zcode5 小时前
运动模糊图像复原的MATLAB仿真与优化
开发语言·matlab
袁雅倩19976 小时前
当吸尘器、筋膜枪都用上Type-C,供电方案该怎么选?浅谈PD取电芯片ECP5702的应用
c语言·开发语言·支持向量机·动态规划·推荐算法·最小二乘法·图搜索算法
Aaswk6 小时前
Java Lambda 表达式与流处理
java·开发语言·python
万邦科技Lafite7 小时前
京东item_get接口实战案例:实时商品价格监控全流程解析
java·开发语言·数据库·python·开放api·淘宝开放平台
王老师青少年编程8 小时前
csp信奥赛C++高频考点专项训练之字符串 --【子串查找】:[NOIP 2009 提高组] 潜伏者
c++·字符串·csp·高频考点·信奥赛·子串查找·潜伏者
Cyber4K8 小时前
【Python专项】进阶语法-系统资源监控与数据采集(1)
开发语言·python·php
初願致夕霞8 小时前
基于系统调用的Linux网络编程——UDP与TCP
linux·网络·c++·tcp/ip·udp
Le_ee8 小时前
ctfweb:php/php短标签/.haccess+图片马/XXE
开发语言·前端·php
yong99909 小时前
MATLAB读取高光谱图像
开发语言·matlab