可能重复: Why does const imply internal linkage in C++, when it doesn’t in C?
如果我有以下情况:
a.cpp:
const int ArrayOfInts[] = {1, 2, 3, 4, 5};b.cpp:
extern const int ArrayOfInts[];
void SomeFunc()
{
int a = ArrayOfInts[0];
}链接器抱怨ArrayOfInts无法从b.obj中得到解决。删除const限定符将使链接成功。知道为什么会失败吗?
谢谢。
发布于 2012-11-14 12:41:04
当编译器编译b.cpp时,尽管它知道,ArrayOfInts[0]的值可以是任何东西。所以它不是编译时间常数。在C++中,文件作用域中的常量默认为编译时常量.
https://stackoverflow.com/questions/13378935
复制相似问题