C ++ header file inclusion problems-include and class **

Source: Internet
Author: User

In the past few days, when a program is created, the basic classes need to be referenced to each other, and there are too many include. As a result, such an unknown error occurs, and finally an include is changed to a class declaration, the C ++ programming specification must be enhanced. The Reprinted below has been slightly changed.

In some large projects, there may be dozens of basic classes, which are inevitably referenced by each other (not satisfying the inheritance relationship, but composite relationship ). That is, mutual declarations are required. Well, this will bring some confusion. If the problem is not handled well, it will make a mess. Based on my experience, I will briefly talk about my own solutions:

When encoding, we usually try to avoid include header files, but use Declaration class XXX. But sometimes the include header file must be used. What is the division of the two?

It should be very clear, but it seems that it is rarely mentioned in the book.

First:
We need to understand why we should replace header file inclusion with declaration: Right, to avoid unnecessary re-compilation (when the header file is changed ). Large projects, low-speed machines, or frequently changing basic classes (unreasonable design), the Compilation speed will still be concerned, and more importantly, the use of declarations can reduce the code (class) this is also a major principle of object-oriented design.

II. General principles:
A. Try to include as few files as possible in the header file. If you can simply declare class clsold; solve the problem, it is best. Reduce Unnecessary include;
B. Try to include as few files as possible. Do not include unused header files.

Iii. When can we simply declare class clsold?
Simply put, you do not need to know how to use the clsold memory layout (except for static members), that is, if it is a pointer or reference method.
Follow these three principles:

(1) Objects of the foo class cannot be defined.

(2) It can be used to define a pointer or reference to this type. (Something of great value)

(3) A function used to declare (not defined) the type as a form parameter or return type.

For example:
Clsold * m_pold; // pointer occupies 4 bytes long
Clsold & Test (clsold * pold) {return * pold };
Everything is OK.

Special cases:
Int test () {return clsold: m_sint ;}
For static member calls, you do not need to know the memory layout, but you need to know that m_sint belongs to the clsold namespace. If you declare only class XXX, it is obviously not enough to explain, therefore, the header file must be included.

To sum up, I have the following suggestions:
1: if there are classes with common dependencies (must include), such as a and B Dependent on D, they can be put together, then, users of the directly include "D" class only need to care about the related types exposed by this class, And the types used internally do not need to be managed (they do not need to include D ). The class provided in this way is better for the caller (you don't need to look up the code to find out if you still need to include other header files ).

2: If Class A depends on Class D and Class B does not rely on Class D, you can separate them into two header files. Include. This avoids unnecessary re-Compilation When d changes.

3: Use pointers or references to call other classes as much as possible, so that only class xxx can be declared. In addition, this is in line with the optimal utilization of resources and is more conducive to the use of polymorphism.

In the past few days, when a program is created, the basic classes need to be referenced to each other, and there are too many include. As a result, such an unknown error occurs, and finally an include is changed to a class declaration, the C ++ programming specification must be enhanced. The Reprinted below has been slightly changed.

In some large projects, there may be dozens of basic classes, which are inevitably referenced by each other (not satisfying the inheritance relationship, but composite relationship ). That is, mutual declarations are required. Well, this will bring some confusion. If the problem is not handled well, it will make a mess. Based on my experience, I will briefly talk about my own solutions:

When encoding, we usually try to avoid include header files, but use Declaration class XXX. But sometimes the include header file must be used. What is the division of the two?

It should be very clear, but it seems that it is rarely mentioned in the book.

First:
We need to understand why we should replace header file inclusion with declaration: Right, to avoid unnecessary re-compilation (when the header file is changed ). Large projects, low-speed machines, or frequently changing basic classes (unreasonable design), the Compilation speed will still be concerned, and more importantly, the use of declarations can reduce the code (class) this is also a major principle of object-oriented design.

II. General principles:
A. Try to include as few files as possible in the header file. If you can simply declare class clsold; solve the problem, it is best. Reduce Unnecessary include;
B. Try to include as few files as possible. Do not include unused header files.

Iii. When can we simply declare class clsold?
Simply put, you do not need to know how to use the clsold memory layout (except for static members), that is, if it is a pointer or reference method.
Follow these three principles:

(1) Objects of the foo class cannot be defined.

(2) It can be used to define a pointer or reference to this type. (Something of great value)

(3) A function used to declare (not defined) the type as a form parameter or return type.

For example:
Clsold * m_pold; // pointer occupies 4 bytes long
Clsold & Test (clsold * pold) {return * pold };
Everything is OK.

Special cases:
Int test () {return clsold: m_sint ;}
For static member calls, you do not need to know the memory layout, but you need to know that m_sint belongs to the clsold namespace. If you declare only class XXX, it is obviously not enough to explain, therefore, the header file must be included.

To sum up, I have the following suggestions:
1: if there are classes with common dependencies (must include), such as a and B Dependent on D, they can be put together, then, users of the directly include "D" class only need to care about the related types exposed by this class, And the types used internally do not need to be managed (they do not need to include D ). The class provided in this way is better for the caller (you don't need to look up the code to find out if you still need to include other header files ).

2: If Class A depends on Class D and Class B does not rely on Class D, you can separate them into two header files. Include. This avoids unnecessary re-Compilation When d changes.

3: Use pointers or references to call other classes as much as possible, so that only class xxx can be declared. In addition, this is in line with the optimal utilization of resources and is more conducive to the use of polymorphism.

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.