The const Modifier

I l @ ve RuBoard

The const Modifier

In C, a global const has external linkage, but in C++ it has internal linkage. That is, the C++ declaration

 const double PI = 3.14159; 

is equivalent to the C declaration

 static const double PI = 3.14159; 

providing both declarations are outside of any function. The C++ rule has the goal of making it simpler to use const in header files. If the constant has internal linkage, then each file that includes the header file gets its own copy of the constant. If a constant has external linkage, then one file has to have a defining declaration and the other files have to have a reference declaration, one that uses the keyword external .

Incidentally, C++ can use the keyword extern to make a const value have external linkage, so both languages can create constants with internal linkage and external linkage. The difference is just in which kind of linkage is used by default.

One additional property of the C++ const is that it can be used to declare an array size :

 const int ARSIZE = 100; double loons[ARSIZE];  /* ok in C++, not in C */ 
I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net