为什么我们将ifdef放在C ++的头文件中定义 [英] Why we put ifdef and define in Header File in C++

查看:97
本文介绍了为什么我们将ifdef放在C ++的头文件中定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么要在C ++中创建头文件时放这些行?

#ifndef TIME1_H
#define TIME1_H

why we put those lines when we are going to make a header file in C++ ?

#ifndef TIME1_H
#define TIME1_H

推荐答案

它是一个包含卫士(请参阅 Wikipedia [^ ]),可以防止同一内容的多个包含标头.
Microsoft编译器(以及其他一些编译器)提供用于解决同一问题的#pragma once指令.
It is an include guard (see Wikipedia[^]), protects against the multiple inclusions of the same header.
The Microsoft compiler (and some others as well) provides the #pragma once directive for addressing the same problem.


这是一种预处理程序,旨在避免重新声明特定项目.在这种情况下,如果在先前加载的标头中定义了TIME_H,则编译器将在重新定义时引发编译错误. #ifndef的简单含义是如果尚未定义",则#define TIME1_H
It is a preprocessor designed to avoid the redeclaration of a specific item. In this case, if TIME_H was defined in a previously loaded header, then the compiler would throw a compilation error on a redefinition. The #ifndef simply means "If this isn''t defined yet" then #define TIME1_H


这篇关于为什么我们将ifdef放在C ++的头文件中定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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