2.9 Writing your own header file
Objective
-
- To allow the program to be separated into separate logical blocks, C + + supports the so-called separate compilation
- The program can consist of multiple files
2.9.1 Design your own header file
-
- Header file provides a centralized location for related claims
- Header files typically contain
- Definition of Class
- Declaration of extern Variable
- of the function
- Use or define the file of the above entity, need to include header file
- How to compile and connect multiple source files
- g++-C main.cc Sales_item.cc-o Main
- Suppose just modified main.cc this source file, that also need to recompile sales_item.cc this file, much trouble
- The compiler provides a way to compile each file separately,
- Usually this process produces an. o file, and an extension of. o indicates that the file contains the target code
- The compiler allows us to connect the target file (. o) together to generate the executable file
- g++-C main.cc
- g++-C sales_item.cc
- g++ MAIN.O Sales_item.o-o Main
- Generated the Mian executable file
First, the header file is used for declaration, not for defining
-
- When designing a header file, it is important to remember the difference between the definition and the Declaration;
- Define only one occurrence
- Declarations can occur multiple times
- The following statements are definitions and should not be placed in the header file
- extern int ival = 10;
- Double fica_rate;
- If the ival variable is redefined in another file, it causes a multiple-definition connection error.
- Because the generic header file is contained in multiple source files, it should not contain the definition of a variable.
- Because in each source file contains the header file, in the actual compilation process, will use the real file content of the header file, to replace the # include "header file"
- If the definition of a variable is included in the header file, then in many source files, there is a duplicate definition of the variable when the content of the header file is replaced.
- However, there are some exceptions, which need to be noted here, as explained in the following:
- The class can be defined in the header file
- Value at compile time, you already know the Const object
- inline function
Second, some const objects are defined in the header file
-
- When the const variable defaults, it is the local space of the file that defines the variable
- A constant expression is a compiler that computes a result expression at compile time.
- Integer literal constants are constant expressions
- This const integer variable can be a constant expression when a const integer variable is self-initialized by a constant expression
- What do you mean
- For example, a const int c = 1;
- The const integer variable is initialized with a constant expression of 1, which can later be assumed to be a const variable of a constant expression
- Any variable in C + + can only be defined once, why?
- Because the storage space is allocated when the variable is defined
- All use of the variable is associated to the same storage space.
- int i = 5;
- A.cc used the I variable, b.cc used the I variable, c.cc used the I variable
- The use of all cc files on variables is associated with the same storage space.
- When a const variable is initialized with a constant expression, you can guarantee that all variables have the same value.
- In fact, in practice, most compilers replace the use of these const variables with corresponding constant expressions.
- So in practice, there is no actual storage space for const variables initialized with constant expressions
- const int i = 5;
- There is no real memory space in memory to store it
- Instead, replace the const int i variable with a constant expression 5来 when used.
- Initialization of a const variable
- Constant expression initialization, const int i = 5;
- const INT J = Func (A, b); For this case,
- Instead of initializing the const variable with a constant expression,
- This definition should not be placed in the header file,
- should be defined and initialized in the source file, just like any other variable.
- The extern declaration should be added to the header file so that it can be shared by multiple files
A brief introduction to the 2.9.2 preprocessor
Objective
-
- #include is part of the C + + preprocessor
- The source code of the preprocessor handler, which runs before the compiler
- Preprocessor---"Compiler---"
- In the source file that actually contains the header file, the preprocessor uses the contents of the header file instead of each # include
- We write our own header files stored in the file, the system header file may be stored in a more efficient way, such as binary??? It's just speculation.
First, header files often require other header files
-
- Header files often include other header files
- It is not uncommon for a header file to be include to the same source file multiple times
- When designing a header file, you should make the header file available for inclusion in the same source file multiple times.
- We must ensure that the classes and objects that contain the same header file multiple times will not cause the header definition to be defined more than once
- How to do the above guarantee? To define a header file protector using a preprocessor
- To avoid re-processing the contents of a header file if it has already been seen
Second, avoid multiple inclusions
-
- The preprocessor allows us to define our own variables
- Preprocessor variables are usually represented in all uppercase letters
- There are two states of preprocessor variables:
- has been defined
- Not defined
- #define接受一个名字, and define the name as the preprocessor variable
- #ifndef detects if the specified preprocessor variable is undefined, and if the preprocessor variable is undefined, then all instructions followed are processed, knowing that the #endif
- For example, you can use the following method to avoid including a header file multiple times
- #ifndef Salesitem_h
- #define Salesitem_h
- .... Defining the Sales_item Class
- #endif
Third, use the custom header file
-
- Header file in <>, that the header file is a standard header file, the compiler will find the header file in a predetermined location, the predetermined location can be set by setting the Find PATH environment variable or command line options to modify
- The header file is seen in "", the header file is considered to be non-system, and the lookup begins at the path where the source file resides.
Chapter II Variables and basic types (8)