If you read the source code of Qt, you will see a bunch of strange macros, such as Q_D and Q_Q. Our Qt source code journey begins with understanding these macros.
The following is an example of C ++.
- class Person
- {
- public:
- Person(){}
- ~Person(){}
- string name();
- void setName(string name);
- int age();
- void setAge(int a);
- private:
- string _name;
- int _age;
- };
This is a very common C ++ class Person, which has two attributes: name and age. Let's imagine how to use this class? If you don't want to give me the source code, at least give me a dll or something similar, and you want to give this header file to me, in this way, I can include it in my code. I can only use the public interface that you expose to me. It is reasonable to say that I should not know private things, but now I know! Why? Because I will read this header file. I know that in Person, age is an int and name is a string. This is what you don't want to see, because since you declare it as private, you just don't want me to know these things. So what should we do? Well, I have a solution. Let's look at the following code:
Person. h
- class Person
- {
- public:
- Person(){}
- ~Person(){}
- string name();
- void setName(string name);
- int age();
- void setAge(int a);
- private:
- PersonPrivateData *data;
- };
Persondata. cpp
- class PersonPrivateData
- {
- public:
- string name;
- int age;
- };
How is it? I can't see how I stored the data in person. h? Well, maybe you are very smart: I can find those statements in persondata. cpp! Of course, this is stipulated by the C ++ syntax, and we can't help it anymore -- but why do I have to give the cpp file to you together? Because you do not need to use the cpp file when using my class library.
This is a way to hide information. It seems very troublesome. The original simple access to name and age had to be accessed through a pointer. Why? In fact, doing so is advantageous:
- Reduce the dependency on header files. In this way, we write all the data members in the cpp file. When we need to modify the data members, we only need to modify the cpp file. Although they are all modified, they are different from modifying the. h file! The reason is that if the. h file changes, the compiler will re-compile all the files that include this. h file. It will take a long time if your class is quite underlying.
- Adds information encapsulation for classes. This means that you cannot see the specific data type at all. You must use getter and setter to access the data. We know that C ++ has a typedef statement. I define a data type ABC. If you don't see a specific file, do you know whether the ABC is string or int?
This is a design method of C ++. It is called Private Class. It is about Private Class! Specifically, it should be a private data class. It is said that this is also the implementation method of Qt 2.x. However, if you look at your Qt SDK code, you will not be able to see such statements. Instead, you will be able to see the macros Q_D that we mentioned at the beginning. Perhaps you have probably guessed that these macros implement this: Private Data.
This article is from the "bean space" blog, please be sure to keep this source http://devbean.blog.51cto.com/448512/325581