How to use include correctly in C + + to reduce the coupling of code

Source: Internet
Author: User

In C + + code, include is required when a file needs to use the classes, structs, constants, and so on that are defined in another file.

When using include, you can have no rules, random include. But the result of this is not looping nesting, which causes the compilation process to become unusually slow or generate a large number of pc-lint alarms.

So, we're going to set some rules for the include.

Before setting these rules, first explain when you need include. To illustrate this, you need to start with the compilation process.

After the program is compiled, executable binary data is generated. In Windows systems, this data is generated into the PE (portable executable) format file. EXE file is a PE format file. The variables and methods defined in the program have their own storage space in the PE file. These variables, methods, and so on are loaded in memory when the PE file is loaded.

From the above, you can conclude that when you compile a program, you must guide the types of parameters in each variable, method, or return value to make it possible to allocate enough memory space at compile time. Therefore, you need to include all types that require parameters at compile time to make it easy to allocate enough memory space for them.

But there is a special case, that is the pointer type variable. During the C + + compilation process, the system allocates only 4 bytes of memory space to the pointer type variable. Therefore, for pointer type variables, include is not required, only class declarations are required.

Good. Here, we can draw the first rule of use for include:

1, only when compiling the program must know its exact storage space variables, you need to include its type in the header file.

2, for pointers to variables, only class declarations, do not need to include header files. Unless some of the interface methods in the pointer variable are used.

3, in addition to the subclass to include the parent class. h files in the. h file, the other include as much as possible in the CPP file.

The above 3 rules are what we should pay attention to when using include. Of these, the first half of the 3rd rule is based on Rule 1; the latter part is based on minimizing unnecessary associations, as described in "effective C + +".

When you write code using include, if you use the above 3 rules, basically you can guarantee that the program will not have too many unrelated associations, to reduce the coupling of the program is very helpful.

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.