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()