Presumably a lot of people have seen it in the papers:#ifndef headername_h
#define Headername_h
This is usually written in a variety of macro definitions, the inclusion of other header files
#endif
The purpose of this is to prevent the header file from being repeatedly referenced .
What do you mean, "header file is repeatedly referenced"?
Answer: actually "Repeated references" refers to the fact that a header file is contained multiple times in the same CPP file, often due to the include caused by nested nesting.
For example: There is a.h file # include "C.h", and B.cpp file also include "A.h" and "# include" C.h ", this will cause c.h to be b.cpp repeated references.
Consequences of repeated references to header files:
Some header file re- references only increase the workload of the compilation work, does not cause too much problem, only is the compilation efficiency is low. But for big projects, it would be a painful task to compile inefficient.
While some header files are included repeatedly, this can cause errors, such as defining global variables in the header file (although this is not recommended, but it is true that the C specification allows it), and this header file repeatedly contains duplicate definitions that cause global variables .
Are all header files to be added to #ifndef/#define/#endif these codes?
Answer: Not necessarily add, but it is best to add! This is because: anyway, with ifnde xxx #define XXX #endif, only the benefits, no harm.
Also, individuals feel that developing a good programming habit is an important aspect of learning programming.
Here is a #ifndef/#define/#endif的格式:
First write: #ifndef a_h (meaning: "If not define A.H", that is, if A.H is not present)
Then write: #define A_H (then introduce A.H)
Last write: #endif (otherwise no need to introduce a.h, i.e. there is already a a.h, no need to introduce again)
Cases:
#ifndef Graphics_h//Prevent Graphics.h from being repeatedly referenced
#define Graphics_h
#include <math.h>//header file referencing the standard library
...
#include "header.h"//header file referencing non-standard libraries
...
void Function1 (...); global function declaration
...
Class Box//struct declaration
{
...
};
#endif
#ifndef headername_h #define HEADERNAME_H #endif use