在C ++标头中定义常量变量 [英] Define constant variables in C++ header

查看:66
本文介绍了在C ++标头中定义常量变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究的程序具有适用于所有类的许多常量。我想制作一个头文件 Constants.h,并能够声明所有相关的常量。然后在其他课程中,我可以只包含 #include Constants.h

A program I am working on has many constants that apply throughout all classes. I want to make one header file "Constants.h", and be able to declare all the relevant constants. Then in my other classes, I can just include #include "Constants.h.

我可以使用它可以使用 #ifndef ... #define ... 语法,但是我更喜欢使用 const int ... 常量形式。我不太确定该怎么做。

I got it to work fine using #ifndef ... #define ... syntax. However, I would prefer to use the const int... form of constants. I'm not quite sure how to though.

推荐答案

您只需在头文件中定义一系列 const ints

You could simply define a series of const ints in a header file:

// Constants.h
#if !defined(MYLIB_CONSTANTS_H)
#define MYLIB_CONSTANTS_H 1

const int a = 100;
const int b = 0x7f;

#endif

之所以可行,是因为在C ++中,名称空间范围内的名称(包括全局名称空间) )的显式声明为const而不显式声明的extern具有内部链接,因此在将翻译单元链接在一起时,这些变量不会导致重复的符号;也可以将常量显式声明为静态。

This works because in C++ a name at namespace scope (including the global namespace) that is explicitly declared const and not explicitly declared extern has internal linkage, so these variables would not cause duplicate symbols when you link together translation units. Alternatively you could explicitly declare the constants as static.

static const int a = 100;
static const int b = 0x7f;

这与C兼容,并且对于可能不熟悉C ++链接规则的人来说更具可读性。

This is more compatible with C and more readable for people that may not be familiar with C++ linkage rules.

如果所有常量都是整数,那么您可以使用的另一种方法是将标识符声明为枚举。

If all the constants are ints then another method you could use is to declare the identifiers as enums.

enum mylib_constants {
    a = 100;
    b = 0x7f;
};

所有这些方法仅使用标题,并允许将声明的名称用作编译时间常数。使用 extern const int 和单独的实现文件可以防止将名称用作编译时间常数。

All of these methods use only a header and allow the declared names to be used as compile time constants. Using extern const int and a separate implementation file prevents the names from being used as compile time constants.

请注意,使某些常量隐式地内部链接 的规则适用于指针,就像其他类型的常量一样。不过,棘手的是,将指针标记为 const 所需的语法与大多数人用来将其他类型的变量设为const的语法略有不同。您需要执行以下操作:

Note that the rule that makes certain constants implicitly internal linkage does apply to pointers, exactly like constants of other types. The tricky thing though is that marking a pointer as const requires syntax a little different that most people use to make variables of other types const. You need to do:

int * const ptr;

创建一个常量指针,以便该规则将适用于它。

to make a constant pointer, so that the rule will apply to it.

还请注意,这是我更喜欢在类型后始终放置 const 的原因之一: int const 而不是 const int 。我还将 * 放在变量旁边:即 int * ptr; 而不是 int * ptr; (还比较讨论)。

Also note that this is one reason I prefer to consistently put const after the type: int const instead of const int. I also put the * next to the variable: i.e. int *ptr; instead of int* ptr; (compare also this discussion).

我喜欢这样做之类的事情,因为它们反映了C ++实际工作的一般情况。替代项( const int int * p )只是特殊情况,以使某些简单的内容更具可读性。问题是,当您退出那些简单的案例时,特殊情况下的替代方案会产生误导。

I like to do these sorts of things because they reflect the general case of how C++ really works. The alternatives (const int, int* p) are just special cased to make some simple things more readable. The problem is that when you step out of those simple cases, the special cased alternatives become actively misleading.

因此,尽管前面的示例显示了 const <的常见用法, / code>,实际上我会建议人们这样写:

So although the earlier examples show the common usage of const, I would actually recommend people write them like this:

int const a = 100;
int const b = 0x7f;

static int const a = 100;
static int const b = 0x7f;

这篇关于在C ++标头中定义常量变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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