Methods In QT 4.6.x
In QT 4.6, the starting point of the class member variable definition method has not changed, but has undergone great changes in the specific implementation methods.
In QT 4.6, a lot of macros are used to do things, which increases the difficulty of understanding the QT source code out of thin air. I don't know if they learned it from MFC. Macro is also widely used to define data variables of class members.
In this version, the class member variable no longer defines a private member for every class, but instead places this common job in the most basic base class qobject, then we defined some related methods for access. Well, let's go to the specific code.
//-------------------------------------
// File name: qobject. h
Class qobjectdata
{
Public:
Virtual ~ Qobjectdata () = 0;
// Omitted
};
Class qobject
{
Q_declare_private (qobject)
Public:
Qobject (qobject * parent = 0 );
Protected:
Qobject (qobjectprivate & DD, qobject * parent = 0 );
Qobjectdata * d_ptr;
}
These codes are in the header file qobject. h. In the definition of qobject class, we can see that the definition of the Data clerk is: qobjectdata * d_ptr; the definition of the protected type is to allow all the derived classes to access this variable, however, this variable cannot be directly accessed externally. The definition of qobjectdata is put in this header file, so that all member variables of the class inherited from qobject must be inherited from the class qobjectdata. The pure virtual destructor determine two things:
* This class cannot be directly instantiated. In other words, if you write such a line of code, new qobjectdata, this line of code will certainly fail, compile cannot pass.
* When the delete pointer variable is directed to any object inherited from qobjectdata, this object can be correctly deleted without generating errors, such, memory leakage.
Let's take a look at what this macro has done. q_declare_private (qobject)
# Define q_declare_private (class )/
Inline class # private * d_func () {return reinterpret_cast <class # private *> (d_ptr );}/
Inline const class # private * d_func () const {return reinterpret_cast <const class # private *> (d_ptr );}/
Friend class # private;
This macro mainly defines two overloaded functions, d_func (), the function is to securely convert the data member variable d_ptr defined in the qobject class to the data member type pointer of each specific class. Let's take a look at the situation after the macro is expanded in the qobject class.
After q_declare_private (qobject) is expanded, the following code is used:
Inline qobjectprivate * d_func () {return reinterpret_cast <qobjectprivate *> (d_ptr );}
Inline const qobjectprivate * d_func () const
{Return reinterpret_cast <const qobjectprivate *> (d_ptr );}/
Friend class qobjectprivate;
After the macro scale was launched, a new problem emerged. Where did qobjectprivate come from? In the qobject class, why don't we directly use qobjectdata for the data member variable type?
Do you remember what we said just now? qobjectdata is a pure virtual function of the class destructor, which indicates that this class cannot be instantiated. Therefore, the actual type of the member variable of the qobject class. This is inherited from qobjectdata, And it is qobjectprivate!
This class stores many very important and interesting things, including qt's core signal and slot data and attribute data. We will explain in detail later, now let's take a look at its definition:
The following is the definition of this class:
Class qobjectprivate: Public qobjectdata
{
Q_declare_public (qobject)
Public:
Qobjectprivate (INT version = qobjectprivateversion );
Virtual ~ Qobjectprivate ();
// Omitted
}
So what is the relationship between qobjectprivate and qobject? How are they associated?
========================================================== ====================================
Statement:
Inside QT series is an original technical article (http://www.qkevin.com.
This series of columns can be reproduced at will, but the statement and original address of each article must be kept.
The Copyright reserved by the author shall not be used for any commercial purposes without the consent of the author
Total index of the column in inside QT series: http://www.qkevin.com/qt
Original address: http://www.qkevin.com/archives/57