C ++包含标准标头的防护 [英] C++ Include Guards for Standard Headers

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

问题描述

我想知道/包括windows.hmath.hiostreamstdio ...等文件的防护措施是什么

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

由于我在不同文件中多次包含了这些标头.这些文件是否已经内置了防护措施?是否定义了定义?

Since I have those headers included multiple times in different files. Do those files already have guards built in or is there a definition defined?

我只是想知道这种事情的标准是什么.

I am just wondering what the standards are for that kind of thing.

推荐答案

如果打开文件以读取内容(甚至可以在大多数编辑器中右键单击include指令以打开文件),则会看到包含文件通常以类似这样的开头:

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_
...

由于未定义_WINDOWS_,因此它将首次进入文件,因此将被定义并包含文件的内容.自先前完成定义以来,第二次#ifndef将失败.

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.

这是放置防护措施的标准方法,许多编译器支持的另一种方法是放置#pragma once.例如,在有人在另一个文件中定义相同常量的情况下,这样做具有防止冲突的优点.

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.

这篇关于C ++包含标准标头的防护的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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