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:
#define GLOBAL_CONST_VAR 0xFF
int GLOBAL_CONST_VAR = 0xFF;
- Some function returing the value (e.g.
int get_GLOBAL_CONST_VAR()
) enum { GLOBAL_CONST_VAR = 0xFF; }
const int GLOBAL_CONST_VAR = 0xFF;
extern const int GLOBAL_CONST_VAR;
and in one source fileconst int GLOBAL_CONST_VAR = 0xFF;
(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_VAR
will 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).
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.
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.- Defining global constant in C++
- shared memory global memory constant memory in cuda
- Defining a Business Add-In
- Python全局变量的隐藏“窍门”(CONSTANT,global)
- global.c
- C#global
- Constant Memory in CUDA
- newline in constant error
- DEFINING TABLE RECORD FORMATS IN HIVE
- global-results in struts2
- Global Hotkeys in WPF
- What's the difference between declaring and defining something in C and C++?(C和C++中,声明和定义的区别)
- unity3d newline in constant error
- unity3d C# Newline in constant
- error C2001: newline in constant
- unity3d newline in constant error
- overflow in implicit constant conversion
- The different between #define and const for a constant in C
- Oracle数据库体系结构
- Inno Setup入门(十)——操作注册表
- 代理模式
- 设计模式之原型模式(深克隆,浅克隆)
- Inno Setup入门(十一)——完成安装后执行某些程序
- Defining global constant in C++
- 基础算法(十一) --- 算法的本质(完)
- Inno Setup入门(十二)——Pascal脚本(1)
- iphone开发必知点之--文件路径
- 在Linux下安装和使用MySQL
- ucGUI内存管理
- linux分支结构if
- Inno Setup入门(十三)——Pascal脚本(2)
- Ilog总结.终结版