Qt core analysis: Information Hiding (1)

Source: Internet
Author: User

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 ++.

 
 
  1. class Person     
  2. {     
  3. public:     
  4.     Person(){}     
  5.     ~Person(){}     
  6.     string name();     
  7.     void setName(string name);     
  8.     int age();     
  9.     void setAge(int a);     
  10. private:     
  11.     string _name;     
  12.     int _age;     
  13. };   

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

 
 
  1. class Person     
  2. {     
  3. public:     
  4.     Person(){}     
  5.     ~Person(){}     
  6.     string name();     
  7.     void setName(string name);     
  8.     int age();     
  9.     void setAge(int a);     
  10. private:     
  11.     PersonPrivateData *data;     
  12. };    

Persondata. cpp

 
 
  1. class PersonPrivateData     
  2. {     
  3. public:     
  4.     string name;     
  5.     int age;     
  6. };   

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

Related Article

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.