在 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;
(也比较 this 讨论).
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
的常见用法,但我实际上建议人们这样编写它们:
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;
相关文章