{ }
C++常量知识点汇总
编程小白也能看懂的常量详解,包含基础概念、使用方法和实用示例
常量基础概念
什么是常量?
常量就是程序运行过程中值不会改变的量。想象一下数学中的π(3.14159…)就是一个常量,它的值固定不变。
为什么使用常量?
- 提高可读性:给固定值一个有意义的名称
- 避免魔法数字:消除代码中含义不明的数字
- 提高安全性:防止意外修改重要数据
- 优化性能:编译器可以对常量进行优化
C++中的常量类型
- 字面常量(Literal Constants)
- const常量(使用const关键字)
- constexpr常量(C++11引入)
- 枚举常量(Enum Constants)
- 宏常量(#define,不推荐)
编程小贴士: 现代C++编程中,优先使用const和constexpr替代#define宏定义常量,因为宏不进行类型检查且容易出错。
字面常量
字面常量是直接在代码中写出的固定值:
// 整数字面常量
42, -7, 0x2A (十六进制), 052 (八进制)
// 浮点字面常量
3.14, -0.5f, 1.23e5
// 字符字面常量
‘A’, ‘\n’, ‘\x41’
// 字符串字面常量
“Hello, World!”, “C++”
42, -7, 0x2A (十六进制), 052 (八进制)
// 浮点字面常量
3.14, -0.5f, 1.23e5
// 字符字面常量
‘A’, ‘\n’, ‘\x41’
// 字符串字面常量
“Hello, World!”, “C++”
字面常量后缀
可以通过后缀指定字面常量的类型:
- u/U:unsigned(无符号)
- l/L:long(长整型)
- f/F:float(单精度浮点)
- ll/LL:long long(长长整型)
123u // unsigned int
3.14f // float
42ll // long long
3.14f // float
42ll // long long
const常量
const关键字用于定义常量,一旦定义后值不可修改:
// 基本数据类型常量
const int MAX_SIZE = 100;
const double PI = 3.14159;
const char NEWLINE = ‘\n’;
// 常量必须初始化
const int MIN_VALUE; // 错误!常量必须初始化
// 尝试修改常量会导致编译错误
MAX_SIZE = 200; // 错误!不能修改常量
const int MAX_SIZE = 100;
const double PI = 3.14159;
const char NEWLINE = ‘\n’;
// 常量必须初始化
const int MIN_VALUE; // 错误!常量必须初始化
// 尝试修改常量会导致编译错误
MAX_SIZE = 200; // 错误!不能修改常量
const与指针
const和指针结合使用有几种不同方式:
指向常量的指针
指针指向的值不能修改
const int* ptr = &var;
*ptr = 10; // 错误!
*ptr = 10; // 错误!
常量指针
指针本身不能修改
int* const ptr = &var;
ptr = &other; // 错误!
ptr = &other; // 错误!
指向常量的常量指针
两者都不能修改
const int* const ptr = &var;
*ptr = 10; // 错误!
ptr = &other; // 错误!
*ptr = 10; // 错误!
ptr = &other; // 错误!
constexpr常量 (C++11)
constexpr 是C++11引入的关键字,用于在编译时计算表达式的值:
constexpr int arraySize = 10; // 编译时常量
int myArray[arraySize]; // 正确
// constexpr函数
constexpr int square(int x) {
return x * x;
}
constexpr int squaredValue = square(5); // 编译时计算
int myArray[arraySize]; // 正确
// constexpr函数
constexpr int square(int x) {
return x * x;
}
constexpr int squaredValue = square(5); // 编译时计算
const vs constexpr
特性 | const | constexpr |
---|---|---|
初始化时机 | 运行时或编译时 | 必须在编译时 |
用于数组大小 | 需要静态const | 可以直接使用 |
函数 | 不能用于函数 | 可以定义constexpr函数 |
C++标准 | C++98 | C++11 |
枚举常量
枚举(enum)提供了一种创建命名常量集合的方式:
// 传统枚举 (C++98)
enum Color {
RED, // 默认为0
GREEN, // 1
BLUE // 2
};
// 强类型枚举 (C++11)
enum class Direction {
UP,
DOWN,
LEFT,
RIGHT
};
// 使用枚举常量
Color c = RED;
Direction d = Direction::UP;
enum Color {
RED, // 默认为0
GREEN, // 1
BLUE // 2
};
// 强类型枚举 (C++11)
enum class Direction {
UP,
DOWN,
LEFT,
RIGHT
};
// 使用枚举常量
Color c = RED;
Direction d = Direction::UP;
枚举的优点
- 提高代码可读性
- 避免使用魔法数字
- 类型安全(特别是enum class)
- 编译器检查有效性
常量成员函数
在类中,常量成员函数承诺不会修改类的成员变量:
class Circle {
private:
double radius;
public:
Circle(double r) : radius(r) {}
// 常量成员函数
double getArea() const {
return 3.14159 * radius * radius;
}
// 非常量成员函数
void setRadius(double r) {
radius = r;
}
};
// 使用
const Circle small(5.0);
double area = small.getArea(); // 正确
small.setRadius(10.0); // 错误!常量对象只能调用常量成员函数
private:
double radius;
public:
Circle(double r) : radius(r) {}
// 常量成员函数
double getArea() const {
return 3.14159 * radius * radius;
}
// 非常量成员函数
void setRadius(double r) {
radius = r;
}
};
// 使用
const Circle small(5.0);
double area = small.getArea(); // 正确
small.setRadius(10.0); // 错误!常量对象只能调用常量成员函数
重要规则: 常量对象(const对象)只能调用常量成员函数,不能调用非常量成员函数。
常量使用最佳实践
const优先
避免宏
constexpr优化
enum代替数字
常量成员函数
- 默认使用const声明不会改变的值
- 在编译期能确定的常量使用constexpr提高性能
- 避免使用#define定义常量(缺乏类型检查和作用域)
- 使用enum class代替传统enum(避免命名冲突)
- 对于不会修改对象状态的成员函数声明为const
- 函数参数中,如果不希望参数被修改,使用const引用或const指针
“合理使用常量是编写健壮、可维护C++代码的关键。它不仅能防止意外修改,还能向其他开发者传达你的设计意图。”