关于 extern 和 const 组合变量的问题
我在互联网上搜索了 const
+ extern
,但对于我的问题似乎没有一个很好的答案。
const
单独意味着内部链接,但是如果我想在编译单元之间共享 const 变量。 extern
是最好的选择吗?
常见的解决方案是:
//g.h
extern const int MAX;
// g.c
extern const int MAX = 3;
但是,这个解决方案有一个缺点,如下所示:
// Say, I want to use this MAX in the same header file.
// g.h
extern const int MAX;
class AClass
{
public:
AClass (): i(MAX){}
private:
int i;
};
编译器会抱怨:“错误C2057:预期的常量表达式”。
有解决办法吗?
I googled const
+ extern
on the internet, but it seems there isn't really a good answer for my question.
const
alone means internal linkage, but if I want to share a const variable among compilation units. Is extern
the best choice?
Common solution would be:
//g.h
extern const int MAX;
// g.c
extern const int MAX = 3;
However, this solution has a drawback, like below:
// Say, I want to use this MAX in the same header file.
// g.h
extern const int MAX;
class AClass
{
public:
AClass (): i(MAX){}
private:
int i;
};
Compiler will complain like:"error C2057: expected constant expression".
Is there a solution for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您希望能够在编译时使用常量(即通过它调整数组大小,而不使用 VLA),则必须在编译时知道它,即它不能具有外部链接。
但是,您可以只在头文件中声明常量,并将其提供给包括它在内的任何人。不过,这不会产生与外部链接完全相同的效果。
if you want to be able to use your constant at compile time (i.e. size an array by it, without using VLA) it has to be known at compile time, i.e. it cannot have external linkage.
However, you could just declare your constant in your header file, and make it available to anyone including it. Still, that won't have the exact same effect as an external linkage.
对于常量整数,一个简单的解决方案是使用枚举:
您将无法获取
MAX
的地址,但反过来您可以以零内存成本获得该地址。An easy solution for constant integers is to use enums:
You won't be able to take the address of
MAX
, but in turn you get this at zero memory cost.做不到。你可以做一些像
完全有效和合法的事情,但是哎呀——不是一个恒定的表达。
Can't be done. You could do something like
Perfectly valid and legal, but whoopsies- not a constant expression.
这是不正确的,
static
表示内部链接,const
只是表示对象不能变异。尝试声明一个变量,因为您的编译器会抱怨链接冲突。要在翻译单元之间共享
const
,请完全按照您的建议进行操作。在标题中
在源文件中
This is not correct,
static
indicates internal linkage,const
just says the object cannot mutate. Try declaring a variable asYour compiler will complain about conflicting linkage. To share a
const
between translation units do exactly what you've suggested.In the header
In the source file
这是一个可以解决您的问题的工作示例。总之,在头文件中将数组大小定义为常量。在另一个头文件中将数组声明为
extern
。在下面的示例中,我将数组引用为extern
,而不使用该数组的包含文件。array_size.hpp
array.cpp
main.cpp
*array_size.hpp* 文件定义大小,该标识符可以在其他翻译单元中使用通过包含标题。
我在 Cygwin 上编译使用:
Here is a working example that may solve your issue. In summary, define the array size as a constant in a header file. In another header file declare the array as
extern
. In the example below I reference the array asextern
without using an include file for the array.array_size.hpp
array.cpp
main.cpp
The *array_size.hpp* file defines the size, the identifier can be used in other translation units by including the header.
I compiled on Cygwin using:
为什么不直接使用#define?
Why not just use a #define?