Inside QT series (II): Object Data Storage ()

Source: Internet
Author: User

Preface: Why do we first talk about this?

We know that in C ++, almost every class requires some class member variables. The common practice is as follows:

Class person
{
PRIVATE:
String mszname; // name
Bool mbsex; // gender
Int mnage; // age
};

When a class is defined, the class member variables are directly defined here, and even the access scope of these member variables is directly defined as public. Are you doing this?

In QT, almost none of them do this. So how does QT do it??

Almost every C ++ class stores a lot of data.CodeYou must know how the data of each class is stored and what it means. Otherwise, we cannot understand other people's c ++ code. In this case, to read the QT code, you must first understand how the QT class member data is saved.

To better understand how QT defines class member variables, let's first talk about the class member variable definition method in QT 2.x, because the method in 2.x is very easy to understand. Next, we will introduce the class member variable definition method in QT 4.6.

Methods In QT 2.x

When defining a class (in. contains only one class member variable. It defines a Member Data Pointer and points it to a data member object, this data member object contains all the member data of this class, and then in the implementation file of the class (. in the CPP file), define this private data member object. The sample code is as follows:

//-------------------------------------
// File name: person. h

Struct personaldatatevate; // declare the private data member type

Class person
{
Public:

Person (); // Constructor
Virtual ~ Person (); // destructor
Void setage (const INT );
Int getage ();

PRIVATE:

Personaldatatevate * D;
};

 

//-------------------------------------
// File name: person. cpp

Struct personaldatatevate // defines the private data member type
{
String mszname; // name
Bool mbsex; // gender
Int mnage; // age
};

// Constructor
Person: Person ()
{
D = new personaldatatevate;
};

// 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 also thought this method was very troublesome, but with the increase in usage, I began to like this method very much, and now I write the code, this method is basically used. Specifically, it has the following advantages:

* Reducing the dependency on header files
Put all the specific data members in the CPP file. In this way, when you need to modify the data members, you only need to modify the CPP file instead of the header file, in this way, we can avoid modifying the header file and re-compiling all the files that contain the file. Especially when the header file is very low-level header files and the project is very large, obvious advantages.
At the same time, this header file also reduces the dependency on other header files. You can include only the data members that need to be used in the CPP file once. The include statement can be minimized in the header file.
* Enhanced class Encapsulation
This method enhances the encapsulation of classes, and cannot directly access class member variables. Instead, you must write the corresponding get/set member functions to do these tasks.
For this question, the benevolent and wise have different opinions. Some people like to define all class member variables as public, which is convenient to use. I personally do not like this method. When a project becomes very large and many people are working on it together, when many people need to use the (# include) underlying code, the disadvantages of this method are fully reflected.

In addition, I do not like to define the variable names of data members as only one letter in QT 2.x. It looks very unintuitive, especially when searching. However, QT kernel does.

So how is it implemented in qt4.6? Please pay attention to the next section.

========================================================== ====================================
Statement:
Inside QT series columnArticleIs (http://www.qkevin.com) original technical article.
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 "Inside QT series:Http://www.qkevin.com/qt
Original address:Http://www.qkevin.com/archives/31

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.