为什么我们将ifdef放在C ++的头文件中定义

为什么我们将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