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()
相关推荐
paeamecium6 分钟前
【PAT甲级真题】- Student List for Course (25)
数据结构·c++·算法·list·pat考试
Lyyaoo.33 分钟前
【JAVA基础面经】抽象类/方法与接口
java·开发语言
0xDevNull37 分钟前
Java实现Redis延迟队列:从原理到高可用架构
java·开发语言·后端
糖炒栗子032638 分钟前
Go 语言环境搭建与版本管理指南 (2026)
开发语言·后端·golang
于先生吖41 分钟前
无人共享健身房 Java 后端源码 + 多端对接完整方案
java·开发语言
cpp_learners1 小时前
银河麒麟V10+飞腾FT-2000/4处理器+QT源码静态编译5.14.2指南
开发语言·qt
野生技术架构师1 小时前
1000道互联网大厂Java岗面试原题解析(八股原理+场景题)
java·开发语言·面试
qqty12172 小时前
Java进阶学习之路
java·开发语言·学习
gCode Teacher 格码致知2 小时前
Javascript提高:get和post等请求,对于汉字和空格信息进行编码的原则-由Deepseek产生
开发语言·前端·javascript·node.js·jquery
黑眼圈子2 小时前
总结一下用Java做算法的常用类和方法
java·开发语言·算法