The header file is a special concept in c/c ++.
First, explain the differences between the Declaration and definition. Extern int x; this is the variable x Declaration, void fun (); this is the function fun () Declaration, class a; this is the class a declaration. Int x; defined by variable x, void fun () {}; is defined by the fun () function, and class a {}; is defined by class.
The header file contains pre-processing commands (commands starting with #), function declarations, classes, struct, joint definitions (Objects cannot be instantiated), and template definitions. Instead of variable definition and function definition. The content in the header file does not generate the target code, and does not allocate memory space for variable declaration or class definition. It is just something declarative. # Replace include with the header file content during the pre-compilation phase.
A compilation unit is a cpp file that can generate a target file. Repeated declarations can exist in the same compilation unit, but repeated variable definitions, function definitions, and class definitions are not allowed. Different compilation units can have repeated declarations and class definitions (this is special, because class definitions are only declarative and do not actually generate the target code, therefore, it can be placed in different compilation units, but cannot be defined repeatedly in the same compilation unit). Repeated variable definitions and function definitions are not allowed.
To prevent duplicate class definitions caused by repeated header file references in the same compilation unit, add the # ifndef # define # endif definition to each header file definition. In addition, this Conditional compilation command can avoid repeated references to header files. In the pre-compilation stage, when the # include command is run, the corresponding header file is expanded, and the header file is also processed when it is expanded. For example, there are three headers, file. h B. h c. h,. h contains # include "B. h ", B. h contains # include "c. h ", c. h contains # include ". h ", this will cause the circular dependency of the file. If there is a file. c, where # include ". h ", that's in. before compiling c files, the Preprocessing Program will constantly copy the content of these three headers. If the content exceeds a certain number, the compilation error of "too many header files" will occur. When # ifndef... # define... # expand. h B. h c. h has defined the macro, to c. # include ". h ", I encountered # ifndef. As this macro was defined during the last expansion, this part was skipped. That is, each header file can be contained only once in each source file.
The following solves an actual problem: both Class A and Class B have pointers of another class, that is, they reference each other. Here we can use the pre-declaration to solve the problem.
Header file a. h
#ifndef A_H#define A_Hclass B;class A{public:B* b;};#endif
Header file B .h
#ifndef B_H#define B_Hclass A;class B{public:A* a;};#endifTwo principles for using header files:
1. If the header file can be excluded, do not include it. At this time, the pre-declaration can solve the problem. If you only use a class pointer and do not use a specific object (non-pointer) of the class or access a specific member of the class, you can use the pre-declaration. Because the size of the pointer data type is specific, the compiler can understand it.
2. Try to include the header file in the CPP file instead of the header file. Assume that A member of Class A is A pointer to Class B. in the header file of Class A, the pre-declaration of Class B is used and the result is cheap, so in the implementation of A, we need to access the specific members of B, so we need to include the header file, then we should in the implementation part of Class A (CPP file) header files containing Class B, not declarative parts