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()
相关推荐
利以檀本人(梦泽不忙)23 分钟前
#T1359. 围成面积
c++·程序人生·算法
汉克老师1 小时前
第十四届蓝桥杯青少组C++国赛[2023.5.28]第二部分编程题(4、 数独填数)
c++·蓝桥杯·蓝桥杯c++·c++蓝桥杯
闻缺陷则喜何志丹1 小时前
【线段树 懒删除堆】P12372 [蓝桥杯 2022 省 Python B] 最优清零方案|普及+
数据结构·c++·线段树·懒删除堆
闻缺陷则喜何志丹1 小时前
【 线段树】P12347 [蓝桥杯 2025 省 A 第二场] 栈与乘积|普及+
数据结构·c++·蓝桥杯·线段树·洛谷
徐归阳1 小时前
数组本身的深入解析
数据结构·c++·算法
Python大数据分析@1 小时前
Python 语言有什么奇技淫巧吗?
开发语言·python·c#
Envyᥫᩣ1 小时前
C#中的设计模式:构建更加优雅的代码
开发语言·c#
C灿灿数模2 小时前
2025全国大学生数学建模C题保姆级思路模型(持续更新):NIPT 的时点选择与胎儿的异常判定
c语言·开发语言·数学建模
白榆!2 小时前
string类的实现
开发语言·c++·算法
励志五个月成为嵌入式糕手2 小时前
0904 类的继承
开发语言·c++