标题保护的目的 [英] Purpose of Header guards

查看:148
本文介绍了标题保护的目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中C ++程序中头标头的目的是什么。

In C++ what is the purpose of header guard in C++ program.

从net i发现这是为了防止包含文件一次又一次, this。

From net i found that is for preventing including files again and again but how do header guard guarantee this.

推荐答案

guard头是为了防止在头文件被包含多次时出现问题;例如

The guard header is to prevent problems if header file is included more than once; e.g.

#ifndef MARKER
#define MARKER
// declarations 
#endif

此文件第一次 #include MARKER 预处理器符号将未定义,因此预处理器将定义该符号,并且以下声明将包含在编译器看到的源代码中。在 #include 的后面,将定义 MARKER 符号,因此 #ifnde / #endif 将被预处理程序删除。

The first time this file is #include-ed, the MARKER preprocessor symbol will be undefined, so the preprocessor will define the symbol, and the following declarations will included in the source code seen by the compiler. On subsequent #include's, the MARKER symbol will be defined, and hence everything within the #ifnde / #endif will be removed by the preprocessor.

要正常工作, MARKER 符号需要对每个头文件可能是 #include -ed不同。

For this to work properly, the MARKER symbol needs to be different for each header file that might possibly be #include-ed.

这种事情的必要性的原因是,在C / C ++中,在编译单元中多次定义具有相同名称的类型或函数是非法的。防护允许头文件 #include 其他头文件,而不必担心这可能会导致一些声明被多次包含。

The reason this kind of thing is necessary is that it is illegal in C / C++ to define a type or function with the same name more than once in a compilation unit. The guard allows a header file to #include other header files without worrying that this might cause some declarations to be included multiple times.

总之,它不会阻止 #include 文件一次又一次。而是允许 不会导致编译错误。

In short, it doesn't prevent you from #include-ing a file again and again. Rather, it allows you to do this without causing compilation errors.

这篇关于标题保护的目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆