Constant
常量(constant) 是指不能改变的值。在 cpp 中,有两种类型常量:
- 命名常量(named constants):有标识符的常量,也叫 符号常量(symbolic constants)
- 字面常量(literal constants):没有标识符的常量
命名常量
在 cpp 中,命名常量有三种子类型:
const
关键词修饰的常量变量constexpr
关键词修饰的编译期(compile-time)常量
#define
定义的字符串替换宏Enumerate
枚举常量
Introduction to constants
In programming, a constant is a value that may not be changed during the program’s execution.[1]
C++ supports two different kinds of constants:
- Named constants are constant values that are associated with an identifier. These are also sometimes called symbolic constants, or occasionally just constants.
- Literal constants are constant values that are not associated with an identifier.
Types of named constants
There are three ways to define a named constant in C++:[1:1]
- Constant variables
- Object-like macros with substitution text
- Enumerated constants
What is literals?
Literals are values that are inserted directly into the code. Literals are sometimes called literal constants because their meaning cannot be redefined.[2]