Click here to Skip to main content
15,917,542 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
In 2 cpp files, If I add a variable definition with the same name respectively.
C++
//1.cpp
const int nTest=1;
...
//2.cpp
const int nTest=1;

...
The complier will build successfully. but if I delete "const " it will report an error!

May the reason is that

C++
const int nTest=1;


is not actually a variable definition ,in other words, The "const int nTest" is not a left-var that has valid memory address, but only a kind of symbol.
But If the reason is really so, the original understand about the "const" keyword is wrong and really confusing me.
My understand about "const" is just to make the variable un-mtuable or un-changeable.

Any Good explanation?
Posted
Updated 25-Jul-12 21:17pm
v2

Const variables can be optimized out by the compiler, if you don't use its address it might not even allocate any storage for it, just inlines the constant value into the code directly. If you don't use const then it can't do this optimization and in your program there will be 2 object files that export the same symbol so it can cause a link time collision. - You can avoid this collision by changing the C++ default public storage specifier by either adding the "static" specifier to at least one of the variables or putting at least one of the variables to an anonymous namespace{} that does the same as the static keyword.
Note that this might be the side effect of a C++ feature that wasn't available for some time, that is you can put a const integer definition into a header file now instead of using a #define. This is true only for integer types. Before this feature everyone was using enums to put integer constants to header files and into class definitions.

EDIT: You might wonder that some other cpp files might want to use this "const int X" with extern, and how would they do it if it isn't exported from its object file at all. You can force exporting it and allocate storage with "extern":

DEFINITION:
C++
extern const int XX = 0;


DECLARATION:
C++
extern const int XX;


EDIT: Constant Values[^]
Somewhere near the beginning of the article:
Quote:
In C, constant values default to external linkage, so they can appear only in source files. In C++, constant values default to internal linkage, which allows them to appear in header files.
 
Share this answer
 
v5
Comments
nv3 26-Jul-12 5:15am    
Interesting! The comment about forcing the definition to be external linkage by the extern keyword is particularly helpful.
pasztorpisti 26-Jul-12 5:22am    
You cant imagine how happy I am about the fact that a lot of ppl don't know about it, because using such externs for variables is usually a bad design pattern. :D Let's write set/get functions instead! A set function can be debugged with a breakpoint, modification of a global variable can only be tracked by data breakpoints that are limited in number and bothersome to use.
The const keyword does not mean immutable, but read-only. Thus, the compiler does define a variable in memory, but prevents your code from applying modifications to it. Other parts of your program that refer to the same memory location might actually change the value while you are not looking.

The reason you get an error at link time when you compile without the const keyword comes from the fact that you are defining the global variable twice. That is so, because the compiler distinguishes a definition of global variable from a mere reference to a global variable by looking if you are initializing the variable with a value or not. By initializing nTest at two places your are trying to define two global variables by that name.

When applying the const keyword you are no longer defining a global variable at all. The const keyword implies "local linkage". That makes sense from view point that you might want to include that definition in a header file, which is included from many .cpp files. So, in your version with the const keyword you are basically defining a file-local constant and hence you don't get an error message when you do that in multiple files and using the same name.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900