为什么#ifndef和#define在c ++头文件中使用 [英] Why are #ifndef and #define used in c++ header files

查看:117
本文介绍了为什么#ifndef和#define在c ++头文件中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常在头文件的开头看到这样的代码

I have been seeing code like this usually in the start of header files

#ifndef HEADERFILE_H
#define HEADERFILE_H

,且档案末尾为

#endif

推荐答案

这些被称为包含警卫

包含标题后,它会检查是否有唯一值(在这种情况下 HEADERFILE_H )。

Once the header is included, it checks if a unique value (in this case HEADERFILE_H) is defined. Then if it's not defined, it defines it and continues to the rest of the page.

当代码再次被包含时,第一个 ifndef 失败,导致一个空白文件。

When the code is included again, the first ifndef fails, resulting in a blank file.

这会阻止对类型,枚举和静态变量等任何标识符的双重声明。

That prevent double declaration of any identifiers such as types, enums and static variables.

这篇关于为什么#ifndef和#define在c ++头文件中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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