Sample code compilation run environment :Windows 64bits+VS2017+Debug+Win32.

The header file is C/C++ An integral part of the process , In use , You should understand the role of header files and related specifications .

<>1. The role of header files

Separate compiling mode is adopted . In a project , More than one source file exists , But they always have something in common , For example, user-defined types , global variable , Declaration of global functions, etc . Extract the contents and put them in the header file , Provide each source file with , It can avoid repeated writing of the same content , Improve programming efficiency and code security . therefore , The main purpose of the header file is to : Provide global variables , The declaration of a global function or the definition of a common data type , So as to separate compilation and code reuse .

In a nutshell , The header file has the following three functions :

(1) Strengthen type inspection , Improve type security .

Use header file , It can effectively ensure the consistency of custom types . although , In grammar , Same data type ( Like a class) Writing multiple times in different source files is allowed , Programmers think they are the same custom type , however , Because the data type does not have an external connection property , The compiler does not care about consistency between multiple versions of the type , This may lead to logical errors . The following procedures were examined .
//source1.cpp #include <iostream> class A { private: char num; public: A();
void show(); }; void A::show(){std::cout<<num<<std::endl;} void see(A&
a){;} //end source1.cpp //source2.cpp #include <iostream> class A {
private: int num; public: A(){num=5;}; void show(); }; void see(A& a); int
main() { A a; see(a); } //end source2.cpp
This program can be successfully compiled and run correctly , In the two source files that make up the project , Yes class
A There is a slight inconsistency in the definition of . In both source files , Member variable num One is char type , One is int type , This results in the output of a special character .

If class A In a header file , be used class A All of the source files contain this header file , It can absolutely guarantee the consistency and security of data types .

(2) Reduce duplication of common code , Improve programming efficiency .

In the process of program development , It is inevitable to modify some data types or interfaces , Use header file , Just modify the contents of the header file , This ensures that the changes take effect in all source files , So as to avoid tedious and error prone repeated modification .

(3) Provide means of confidentiality and code reuse .

So is the header file C++ An indispensable means in code reuse mechanism , On many occasions , Source code inconvenience ( Or not ) Publish to users , Just provide the user with the header file and binary library . The user only needs to call the library function according to the interface declaration of the header file , You don't have to care about the concrete implementation of the interface , The compiler connects the corresponding implementation code from the library .

<>2. Usage of header file

<>2.1 Content of header file

The header file contains the common content of multiple source files , therefore , Global function prototype declaration , Global variable declaration , Custom macros and types should be placed in the header file . The header file of the specification is allowed to be included by multiple source files without causing compilation errors , So the definition of global variables , Definition of external variables , Definition of global function , The definition of class member functions outside the class body that can only appear once should not be placed in the header file .

<>2.2 Use the header file provided by the system

C The header files provided by the .h Ending , Such as standard library header file stdio.h etc .C++ The original purpose of language is to be a “ better C”, therefore C++ The language is still in use C
Naming conventions of language header files , Add header file after .h sign . along with C++ Language development ,C++ A new standard library has been added , In order to avoid C There is a conflict ,C++
Name space is introduced to avoid name conflict , The header file is also removed .h suffix . therefore , For a while , Many header files have two versions , One with .h
ending , The other is not , as iostream.h( In global namespace ) and iostream( In the namespace std). Programmers have different options for programming , quite a lot C++
The source program begins with such a statement :
#include <iostream.h>
And others , Start with these two statements :
#include <iostream> using namespace std;
This phenomenon is somewhat confusing , therefore C++ Standards Committee regulations , used C Header file ( as stdio.h) and C++ Zhongxin C Header file ( as cstdio) Continue to use , But the old one C++
Header file ( as iostream.h) It has been abandoned , All adopted C++ Header files specified in the new standard ( as
iostream). in addition , When the system header file is included , Should be used <>( Angle brackets ) instead of ””( Double quotation marks ). For example, you should include the header file like this iostream:
#include <iostream>
It's not like this :
#include “iostream”

Double quotation marks ”” Used to contain custom header files , It is a bad programming habit to use it to include system header files . The reason is that the compiler encountered a header file wrapped in double quotation marks, which is the user-defined header file by default , Search from project directory , If you can't find it, you will find it in the system directory , If there is a user-defined header file with the same name as the system header file , Errors that do not meet expectations will occur .

<>2.3 Avoid repeated inclusion of header files

in , Such as the definition of global variables , The definition of global functions can only appear once in a project . Some can appear many times , But it can only appear once in a source file , as class And so on , Others can appear more than once in a source file , Such as function declaration . The content of the header file cannot be determined in advance , You should avoid including the same header file more than once in a source file , In order to avoid redefinition errors . The following procedures were examined .
//header1.h class A { int num; public: A(); void show(); }; //end header1.h
//header2.h #include “header1.h” class B { A a; public: void disp(); }; //end
header2.h //main.cpp #include <iostream> #include “header1.h” #include
“header2.h” A::A() { num=5; } void A::show(){std::cout<<num<<std::endl;} int
main() { A a;; } //end main.cpp
This program cannot be compiled , as a result of class A In the source file main.cpp Is defined twice , This is due to the header file header2.h Contains
header.1, In the source file main.cpp Contains header2.h, It also includes header1.h, This leads to header1.h stay main.cpp
It's included twice , That's what happened class A Repetitive definition .

It is common for a header file to be repeatedly included by another source file , How to avoid repeated inclusion of a header file ? Easy solution with conditional compilation . Add at the beginning of the header file :
Add at the end of the header file :
Can be any content , As long as the current header file can be uniquely identified , The name of the header file is recommended . Add these conditional compilation preprocessing instructions to the two header files in the sample program above , The problem can be solved . in addition , It can also be used
#paragma once Preprocessing instructions , But this method is not supported by all compilers , Considering the portability of the code , Conditional compilation preprocessing instructions are recommended .

Read the sample code above , The following points need to be noted :
(1) Conditional compilation instruction #ifndef HEADER_NAME and #endif It means : If the conditional compilation flag HEADER_NAME If there is no definition , Then compile #ifndef
and #endif Between the program segments , Otherwise, ignore it . Header file header1.h Just be included once , Conditional compilation flag macro HEADER_NAME Will be defined , So it won't be included again .

(2)iostream Is the header file in the standard library , So use angle brackets when included <>, and header1.h and header2.h
Is a user-defined header file , Use double quotation marks when included .

<> reference

[1] Chen Gang .C++ Advanced course [M]. Wuhan : wuhan university press ,2008.C2-10 About header files .P82-87

©2020 ioDraw All rights reserved
Sharing a script of hacker empire code rain , Simple and fun !-bash The Trojan horse , Have you ever met one ?k8s delete pod Zhejiang University data structure midterm examination questions Detailed explanation PHP Medium die,exit,returnlayui.table Examples of dynamically getting header and list data Interview questions : Handwritten list ( Include reverse linked list ) After the outbreak Which programming has a bright future PTA Quick sort Ctrip 2019 School recruitment LRU Cache