最佳答案
所以我最近在我工作的地方进行了一次讨论,在这次讨论中,我质疑使用 双倍包含后卫对单个后卫的使用。我所说的 双重保护是这样的:
头文件“ Header _ a. hpp”:
#ifndef __HEADER_A_HPP__
#define __HEADER_A_HPP__
...
...
#endif
在任何地方包含头文件时,无论是在头文件还是源文件中:
#ifndef __HEADER_A_HPP__
#include "header_a.hpp"
#endif
现在我明白了,在头文件中使用约束是为了防止多次包含已经定义的头文件,这是很常见的,并且有很好的文档说明。如果宏已经定义,编译器会将整个头文件视为“空白”,并防止重复包含。很简单。
The issue I don't understand is using #ifndef __HEADER_A_HPP__
and #endif
around the #include "header_a.hpp"
. I'm told by the coworker that this adds a second layer of protection to inclusions but I fail to see how that second layer is even useful if the first layer absolutely does the job (or does it?).
我能想到的唯一好处是,它可以直接阻止链接器找到文件。这是为了提高编译时间(没有提到这是一个好处) ,还是有什么其他的东西在这里起作用,我没有看到?