C++ Include Guards for Standard Headers

后端 未结 4 1729
悲哀的现实
悲哀的现实 2021-01-13 00:36

I am wondering if/ what include guards on files like windows.h, math.h, iostream, stdio... etc.

Since I have thos

4条回答
  •  执念已碎
    2021-01-13 01:14

    If you open the file to read the contents (you can even right click the include directive in most editors to open the file), you will see that include files usually start with something like:

    #ifndef _WINDOWS_
    #define _WINDOWS_
    ...
    

    So the first time it will go in the file since _WINDOWS_ is not defined, therefore it will be defined and the contents of the file will be included. The second time the #ifndef will fail since the define was done previously.

    This is the standard way to put a safeguard, another way which is supported by many compilers is to put #pragma once. This has the advantage to prevent collision in the case someone would define the same constant in another file for example.

提交回复
热议问题