Is that the class member variables are defined here directly in the class definition, and even if the access scope of these member variables is directly defined as public, are you doing this?
In Qt, it's almost not the case, so what does QT do?
Almost every C + + class will save a lot of data, to read someone else's written C + + code, it is necessary to know how each class of data is stored, what meaning, otherwise, we can not read someone else's C + + code. In this case, in order to read QT code, the first step is to understand how Qt's class member data is stored.
To make it easier to understand how Qt defines class member variables, let's start with the class member variable definition method in the QT 2.x version, because the method in 2.x is very easy to understand. Then describe the class member variable definition method in QT 4.4.
QT 2.x the methods in
When defining class (in the. h file), there is only one class member variable, only one member data pointer is defined, and then this pointer points to a data member object that contains all of the class's member data, and then in the class implementation file (. cpp file) , define this private data member object. The sample code is as follows:
//------------------------------------------------------------------------------------------------------------- -
//File name:person.h
struct personaldataprivate;// declaring private data member types
Class Person
{
Public
Person (); Constructor
Virtual ~person (); destructor
void setage (const int);
int Getage ();
Private
personaldataprivate* D;
};
//------------------------------------------------------------------------------------------------------------- -
//File name:person.cpp
struct personaldataprivate// defining private data member types
{
String Mszname; Name
BOOL Mbsex; Gender
int mnage; Age
};
Constructor
Person::P Erson ()
{
d = new Personaldataprivate;
};
destructor
Person::~person ()
{
Delete D;
};
void Person::setage (const int age)
{
if (age! = d->mnage)
D->mnage = age;
}
int Person::getage ()
{
Return d->mnage;
}
When I first learned QT, I thought it was a lot of trouble, but as the use increased, I started to like it, and now I'm writing code that basically uses this method. Specifically, it has the following advantages:
Reduce the dependency of the header file
Put the specific data members into the CPP file, so that when you need to modify the data members, only need to change the CPP file without the need for a header file, so you can avoid the first time because of the changes in the header file, all the files containing this file are all recompiled once, Especially when this header file is very low on the header file and the project is very large, the advantages are obvious.
It also reduces the dependency of the header file on other header files. You can include only one time in the CPP file that is needed in the data member, and you can reduce the include statement as much as possible in the header file
Encapsulation of enhanced classes
This method enhances the encapsulation of the class, and can no longer directly access the class member variables, but must write the corresponding Get/set member function to do these things.
Everyone has a different point of view on this issue, benevolent see and the other. Some people just like to define a class member variable as public, which is convenient when used. But I personally do not like this method, when the project becomes very large, there are a lot of people in the project together when the code is written in the bottom of a lot of people need to use (#include), the shortcomings of this method is fully reflected.
And, I don't like QT 2.x. The variable names of data members are defined as only one letter, D, which seems to be not intuitive, especially when search is inconvenient. But that's exactly what the QT kernel did.
So, how is it implemented in the latest QT4? Please follow the next section.
====================================
Statement:
The Inside QT Series column is the original technical article of the QT Core Technology Forum (insideqt.com).
This series of columns may be reproduced at will, but this paragraph and the original address of each article must be retained.
Not for commercial use without the consent of the author
"Inside Qt Series" Column Total index:
Http://www.insideqt.com/bbs/viewthread.php?tid=9&extra=page%3D1
Original address of this article:
Http://www.insideqt.com/bbs/viewthread.php?tid=5&extra=page%3D1
====================================