如何在C ++中正确命名包括Guards的名称 [英] How to properly name include guards in c++

查看:38
本文介绍了如何在C ++中正确命名包括Guards的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个头文件:

misc.h
MyForm.h
Registration.h

在我放入的misc.h文件中

In misc.h file I put

#ifndef MISC_H
#define MISC_H
#endif

在MyForm.h中,我放

In MyForm.h, I put

#include "misc.h"
#include "Registration.h"

然后在Registration.h中输入

And in Registration.h, I put

#include "misc.h"

感谢您的帮助.

推荐答案

如何命名包括警卫人员的意见基于观点,但可以提出一些建议:

How to name include guards is somewhat opinion based, but a few recommendations can be made:

1)不要简短,因为它们很可能会与其他文件或您所使用的实现或库中定义的其他宏发生冲突.

1) Don't make them short as they are then likely to clash with other macros defined in other files or the implementation or libraries you use.

2)使它们包含项目特定的前缀,以使它们唯一且明确.

2) Make them contain a project specific prefix in an attempt to make them unique and unambiguous.

3)如果您的项目包含多个子部分模块"/库"等,请使保护名称包含模块名称,以使其在您的项目中具有唯一性-还应包含文件名.

3) If your project contains multiple sub-parts "modules"/"libraries" etc, make the guard name contain the name of the module to make them unique within your project - include the file name as well.

4)确保不要使用诸如 NULL 之类的保留名称,以数字开头的名称或以下划线后跟大写字母开头的名称以及包含连续下划线的名称;所有这些名称都保留给实现使用.

4) Make sure not to use reserved names such as NULL, names starting with a number or names that begin with an underscore followed by an uppercase letter as well as names containing consecutive underscores; all such names are reserved for the implementation to use.

5)将所有大写字母用于宏(只是一个通用约定).

5) Use all uppercase for macros (just a common convention).

对于子模块"bar"中名为"foo"的项目中名为"baz.h"的文件,我将其命名为 FOO_BAR_BAZ_H .

For a file named "baz.h" in a project named "foo", in submodule "bar", I'd name the guard FOO_BAR_BAZ_H.

PS.确保您的标头使用的 all 全部包括防护(在非常特殊和罕见情况下除外).否则,您会遇到多个定义错误以及更多错误的麻烦.

PS. Make sure all your headers use include guards (except in very special and rare cases). Otherwise you are inviting trouble with multiple definition errors and more.

这篇关于如何在C ++中正确命名包括Guards的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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