# Ifdnef, # ifdef, # Usage Analysis of if
# Ifdnef, # ifdef, # If are all commands used to prevent repeated compilation.
Conditional compilation is provided for the processing program.
Common preprocessing commands are described as follows:
I. # include [**. H]
The specified file is inserted into the code of the instruction location. It is usually used to include header files, which can be defined elsewhere by code.
Ii. # define [Key] [key_value]
Key_value is used to replace each occurrence of a key.
Note: # define (macro definition) is a "Dummies" replacement, so it is often very dangerous. In C ++, const is used to define constants, achieving the same performance as # define, and more powerful than # define. When defining constants, const can specify the type of constant variables.
Iii. # ifdef identifier
Procedure 1
# Else
Procedure 2
# Endif
Its function is to operate (or execute, or insert program code to the position specified by # include during preprocessing) if the identifier is true; otherwise, Segment 2 is executed. At this time, the program is compiled with either of the following options:
Note: # else does not exist.
IV: # ifndef identifier
Procedure 1
# Endif
Its function is: if the identifier has not been defined, that is, the identifier is false, then the procedure segment 1 (that is, the program segment between # ifndef and # endif ).
The method to prevent repeated file inclusion is as follows:
Defines a class. h file. The function of this file is to declare a user-defined class.
Define a class. cpp file. The function of this file is to define a user-defined class, that is, to implement the function of member functions in each class in this file.
Define a main. cpp file, which is the main function file, that is, the program entry file.
The class. cpp file and the main. cpp file respectively contain the class. h file. To prevent the class. h file from being repeatedly included, you can write the code in class. h as follows:
# Ifndef identifier
# Define identifier
Program Section
# Endif
In this way, when
Class. CPP and Main. the CPP file contains class for the first time. the identifier is not defined when the H header file is used. In this case, the Preprocessing Program inserts all the code between "# ifndef identifier" and "# endif" into # includes <class. h> location. When class. H is included for the second time, the identifier has been defined. Therefore, the code between "# ifndef identifier" and "# endif" is skipped to avoid repeated inclusion.
Note: To avoid repeated inclusion, the "identifier" generally uses the name of the header file, where "funny" is replaced by "underline.