https://blog.csdn.net/conepoint/article/details/50635470
- class Person
- {
- public:
- Person(){}
- ~Person(){}
- string name();
- void setName(string name);
- int age();
- void setAge(int a);
- private:
- string _name;
- int _age;
- };
- class Person
- {
- public:
- Person(){}
- ~Person(){}
- string name();
- void setName(string name);
- int age();
- void setAge(int a);
- private:
- PersonPrivateData *data;
- };
- class PersonPrivateData
- {
- public:
- string name;
- int age;
- };
- 减少头文件的依赖。像这样我们把数据成员都写在 cpp 文件中,当我们需要修改数据成员的时候就只需要修改 cpp 文件。虽然都是修改,但这和修改 .h 文件是不一样的!原因在于,如果 .h 文件发生改变,编译器会重新编译所有 include 了这个 .h 文件的文件。如果你这个类相当底层,那就会花费很长时间。
- 增加类的信息封装。这意味着你根本看不到具体数据类型,必须使用 getter 和 setter 去访问。我们知道 C++ 有一个 typedef 语句,我定义一个数据类型 ABC,如果你看不到具体文件,你会知道这个 ABC 是 string 还是 int 么?
- void MyClass::setFoo( int i )
- {
- Q_D(MyClass);
- d->m_foo = i;
- }
- int MyClass::foo() const
- {
- Q_D(const MyClass);
- return d->m_foo;
- }
- class MyObject: public QObject
- {
- Q_OBJECT
- public:
- MyObject();
- virtual ~ MyObject();
- void setMemberX( int x );
- int memberX() const;
- void setMemberY( double y);
- double memberY() const;
- signals:
- void priorityChanged( MyObject::Priority priority );
- private:
- int m_memberX;
- double m_memberY;
- };
- class MyObjectPrivate;
- class MyObject: public QObject
- {
- Q_OBJECT
- public:
- MyObject();
- virtual ~ MyObject();
- void setMemberX( int x );
- int memberX() const;
- void setMemberY( double y);
- double memberY() const;
- signals:
- void priorityChanged( MyObject::Priority priority );
- protected:
- MyObjectPrivate * const d_ptr;
- private:
- Q_DECLARE_PRIVATE(MyObject);
- };
- class MyClassPrivate;
- #include "myclass_p.h"
- #define Q_DECLARE_PRIVATE(Class)
- inline Class##Private* d_func() { return reinterpret_cast(qGetPtrHelper(d_ptr)); }
- inline const Class##Private* d_func() const { return reinterpret_cast(qGetPtrHelper(d_ptr)); }
- friend class Class##Private;
- inline MyClassPrivate* d_func() { return reinterpret_cast(qGetPtrHelper(d_ptr)); }
- inline const MyClassPrivate* d_func() const { return reinterpret_cast(qGetPtrHelper(d_ptr)); }
- friend class MyClassPrivate;
- // A template function for getting the instance to your private class instance.
- template static inline T *qGetPtrHelper(T *ptr) { return ptr; }
- // A macro for getting the d-pointer
- #define Q_D(Class) Class##Private * const d = d_func()
- MyClassPrivate * const d = d_func()
- class MyObjectPrivate
- {
- public:
- MyObjectPrivate(MyObject * parent):
- q_ptr( parent ),
- m_priority(MyObject::Low)
- {}
- void foo()
- {
- // Demonstrate how to make MyObject to emit a signal
- Q_Q(MyObject);
- emit q->priorityChanged( m_priority );
- }
- // Members
- MyObject * const q_ptr;
- Q_DECLARE_PUBLIC(MyObject);
- MyObject::Priority m_priority;
- };
- QObjectData {
- public:
- virtual ~QObjectData() = 0;
- // others
- };
- class QObject
- {
- Q_DECLARE_PRIVATE(QObject)
- public:
- Q_INVOKABLE explicit QObject(QObject *parent=0);
- protected:
- QObject(QObjectPrivate &dd, QObject *parent = 0);
- QScopedPointer<QObjectData> d_ptr;
- // others
- }
- QScopedPointer<QObjectData> d_ptr;
- QObjectData *d_ptr;
- #define Q_DECLARE_PRIVATE(Class)
- inline Class##Private* d_func() { return reinterpret_cast(qGetPtrHelper(d_ptr)); }
- inline const Class##Private* d_func() const { return reinterpret_cast(qGetPtrHelper(d_ptr)); }
- friend class Class##Private;
- inline QObjectPrivate* d_func() { return reinterpret_cast(qGetPtrHelper(d_ptr)); }
- inline const QObjectPrivate* d_func() const { return reinterpret_cast(qGetPtrHelper(d_ptr)); }
- friend class QObjectPrivate;
- class Q_CORE_EXPORT QObjectPrivate : public QObjectData
- {
- Q_DECLARE_PUBLIC(QObject)
- public:
- QObjectPrivate(int version = QObjectPrivateVersion);
- virtual ~QObjectPrivate();
- // others
- }
- QObject::QObject(QObject *parent)
- : d_ptr(new QObjectPrivate)
- {
- // others
- }
- QObject::QObject(QObjectPrivate &dd, QObject *parent)
- : d_ptr(&dd)
- {
- // others
- }
- class QWidget : public QObject, public QPaintDevice
- {
- Q_OBJECT
- Q_DECLARE_PRIVATE(QWidget)
- // others
- }
- QWidget::QWidget(QWidget *parent, Qt::WindowFlags f)
- : QObject(*new QWidgetPrivate, 0), QPaintDevice()
- {
- QT_TRY {
- d_func()->init(parent, f);
- } QT_CATCH(...) {
- QWidgetExceptionCleaner::cleanup(this, d_func());
- QT_RETHROW;
- }
- }