Defining global constant in C++

来源:互联网 发布:敏感肌肤如何修复知乎 编辑:程序博客网 时间:2024/06/08 14:03

 To define a constant in C++ to be visible in several source files. I can image the following ways to define it in a header file:

  1. #define GLOBAL_CONST_VAR 0xFF
  2. int GLOBAL_CONST_VAR = 0xFF;
  3. Some function returing the value (e.g. int get_GLOBAL_CONST_VAR())
  4. enum { GLOBAL_CONST_VAR = 0xFF; }
  5. const int GLOBAL_CONST_VAR = 0xFF;
  6. extern const int GLOBAL_CONST_VAR; and in one source file const int GLOBAL_CONST_VAR = 0xFF;
The most reasonable opinions as belows,
1.

(5) is "better" than (6) because it defines GLOBAL_CONST_VAR as an Integral Constant Expression (ICE) in all translation units. For example, you will be able to use it as array size and as case label in all translation units. In case of (6) GLOBAL_CONST_VAR will be an ICE only in that translation unit where it is defined and only after the point of definition. In other translation units it won't work as ICE.

However, keep in mind that (5) gives GLOBAL_CONST_VAR internal linkage, meaning that the "address identity" of GLOBAL_CONST_VAR will be different in each translation unit, i.e. the &GLOBAL_CONST_VARwill give you a different pointer value in each translation unit. In most usage cases this doesn't matter, but if you'll need a constant object that has consistent global "address identity", then you'd have to go with (6), sacrificing the ICE-ness of the constant in the process.

Also, when the ICE-ness of the constant is not an issue (not an integral type) and the size of the type grows larger (not a scalar type), then (6) usually becomes a better approach than (5).

(2) is not OK because the GLOBAL_CONST_VAR in (2) has external linkage by default. If you put it in header file, you'll usually end up with multiple definitions of GLOBAL_CONST_VAR, which is an error.const objects in C++ have internal linkage by default, which is why (5) works (and which is why, as I said above, you get a separate, independent GLOBAL_CONST_VAR in each translation unit).

 
2.

It depends on your requirements. (5) is the best for most normal usage, but often results in the constant taking up storage space in every object file. (6) can get around this in situations where it's important.

(4) is also a decent choice if your priority is guaranteeing that storage space is never allocated, but it only works for integral constants of course.


3.
(2) is illegal because it violates the One Definition Rule. It defines GLOBAL_CONST_VAR in every file where it's included, i.e. more than once. (5) is legal because it's not subject to the One Definition Rule. Each GLOBAL_CONST_VAR is a separate definition, local to that file where it's included. All those definitions share the same name and value of course, but their addresses could differ.

Transfer from:
http://stackoverflow.com/questions/2268749/defining-global-constant-in-c


Another, comments from <<Thinking in C++>>
In C++, a const doesn't necessarily create storage. In C a const always creates storage. Wheater or not storage is reserved for a const in C++ depends on how it is used. In general, if a const is used simply to replace a name with a value(just as you would use a #define), then storage doesn't have to be created for the const. If no storage is created(this depends on the complexity of the data type and the sophistication of the compliler), the values may be folded into the code for greater efficiency after type checking, not before, as with #define. If, however, you take an address of a const(even unknowingly, by passing it to a function that takes a reference argument) or you define it as extern, then the storage is created for the const.


Supplement:
If we want to define a global constant string in c++ on header files,should be like this

const char* const GLOBAL_CONST_STRING = "global const string";
const char GLOBAL_CONST_STRING[] = "global const string";