为什么要加入守卫?

编程入门 行业动态 更新时间:2024-10-25 10:28:49
本文介绍了为什么要加入守卫?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

包含守卫,如此处所定义,用于防止两次加载相同的代码编译.

Include guards, as defined here, are used to prevent loading the same code twice at compilation.

为什么我的编译器 (GCC) 无法检测到它正在加载相同的代码两次并且具有合理的默认行为?

Why can't my compiler (GCC) detect that it is loading the same code twice and have a sensible default behaviour?

推荐答案

很简单,因为您可能希望编译器加载该文件两次.

Simply because you might have wanted the compiler to load that file twice.

请记住,#include 只是加载一个文件并将其内容放在指令的位置.该文件可能是头文件,但也可能是有用且经常使用的源代码.

Remember, that #include simply loads a file and puts its contents in the place of the directive. This file might be a header file, but may be useful and frequently used piece of source code as well.

大多数现代编译器对 #pragma once 做出反应,完全按照您的意愿行事.但请记住,这是一个未包含在语言规范中的编译器扩展,坚持包含保护通常是个好主意 - 您会确定它适用于所有编译器和任何情况.

Most modern compilers react to #pragma once doing exactly what you want them to. Remember though, that this is a compiler extension not included in the language specification and it is generally a good idea to stick to include guards - you'll be certain, that it works on every compiler and in any circumstances.

更多推荐

为什么要加入守卫?

本文发布于:2023-10-04 02:51:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1467107.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!