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.

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

You can make a constant at file scope with external linkage by declaring it extern const.
I'll add "by default" to the end of my answer.
"In C++, constants at file scope are compile-time constants by default." - thought this only applied to simple integer types. Thanks for clarifying.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.