2

Possible Duplicate:
Why does const imply internal linkage in C++, when it doesn’t in C?

If I have the following:

a.cpp:

const int ArrayOfInts[] = {1, 2, 3, 4, 5};

b.cpp:

extern const int ArrayOfInts[];

void SomeFunc()
{
    int a = ArrayOfInts[0];
}

The linker complains that ArrayOfInts is unresolved from b.obj. Removing the const qualifier makes the link succeed. Any ideas why this fails?

Thanks.

Community
  • 1
  • 1
Badder
  • 35
  • 1
  • 3

1 Answers1

0

When the compiler compiles b.cpp, for all it knows, the value of ArrayOfInts[0] could be anything. So it's not a compile-time constant. In C++, constants at file scope are compile-time constants by default.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278