You may not be familiar with it or care about it. We only care about whether the program runs correctly or how it is implemented.
Here I will introduce the "# include" that we are familiar with and not familiar with. First, we will understand the C/C ++ header file.
The header file provides a centralized location for relevant declarations. The header file generally contains the class definition, extern variable declaration, and function declaration. Note the difference between declaration and definition: the essential difference between them is that the definition can only appear once, and the declaration can appear multiple times. Declare that no space is allocated, and define that the space is to be allocated. The correct use of header files ensures that all files use the same declaration of the given object. When the Declaration needs to be modified, only the header file needs to be updated.
The header file can also be defined: the cosnt object and inline function with its value are known during compilation. The above entity is defined in the header file because the compiler needs their definition to generate code. For example, to generate object code that can define or use classes, the compiler needs to know the data members of this type and the corresponding functions.
In C ++, constant expressions must be placed in some places. For example, the initialization of enumeration members must be a constant expression. Constant expressions are expressions that can be computed by the compiler during compilation. When a const integer variable is initialized through a constant expression, this const integer variable may be a constant expression. To make the const variable a constant expression, the initialization must be visible to the compiler. To allow multiple files to use the same constant value, the const variable and its initialization must be visible to each file. Therefore, the definition is put into the header file. No matter when the const constant is used, the compiler can know its initialization.
# How does include work?
# The include facility is part of the C ++ Preprocessor.
The source code of the Preprocessor is run before the compiler.
# Include only accepts one parameter: header file name.
The Preprocessor replaces each include with the content of the specified header file.
Our own header files are stored in files. System header files may be saved in a more efficient format specific to the compiler.
Header files often # include other header files. Objects defined in header files are often used in other file facilities. Therefore, the header file should be included in the same source file multiple times. We must ensure that the class and object defined by the header file will not be defined multiple times if the same header file is included. To ensure the security of header files, use the pre-processor to define header file protection characters.
What is a header file protection character?
You will understand the following content.
Before writing header files, we need to introduce some additional pre-processor facilities. The Preprocessor allows us to customize variables. To avoid name conflicts, Preprocessor variables are often expressed in uppercase letters. Preprocessing variables have two states: defined or undefined.Copy codeThe Code is as follows: # ifndef BEGEIN_H
# Define BEGEIN_H
// Define some classes or functions
# Endif
If there are no two header files defining and using the same Preprocessor variable, then there is no problem. If the preceding problem occurs, you can use the entity class defined in the header file to name the Preprocessor variable to avoid the cognominal problem of the Preprocessor variable.
# Include <> and # include ""
If the header file name is in <>, it is considered as a standard header file. The compiler looks for this header file in a predefined position. If it is "", it is considered as a non-system header file. The non-system file search usually starts from the path where the source file is located.