在 C++ 中,const 关键字用于指定一个对象或变量是常量,意味着它的值在初始化之后不能被修改。下面详细介绍 const 修饰变量、指针、类对象和类中成员函数的区别以及注意事项。
修饰变量
详细介绍
当 const 修饰变量时,该变量成为常量,在定义时必须进行初始化,并且之后不能再对其进行赋值操作。这有助于保证数据的安全性和程序的可读性。
使用示例
#include <iostream>
int main() {
const int num = 10;
// num = 20; // 错误:不能对常量进行赋值操作
std::cout << "The value of num is: " << num << std::endl;
return 0;
}
在上述代码中,num 被声明为 const 常量,初始化后不能再修改其值。
注意事项
- 初始化要求:const 变量必须在定义时进行初始化,否则会导致编译错误。
- 作用域:const 变量的作用域遵循普通变量的作用域规则,但其值不可修改。
修饰指针
const 修饰指针有多种情况,下面分别介绍。
1. 常量指针(指针指向的内容是常量)
#include <iostream>
int main() {
int num = 10;
const int* ptr = #
// *ptr = 20; // 错误:不能通过指针修改所指向的内容
int anotherNum = 20;
ptr = &anotherNum; // 可以修改指针指向的地址
return 0;
}
在这种情况下,const 位于 * 左侧,表示指针指向的内容是常量,不能通过该指针修改所指向的内容,但可以修改指针本身的值,使其指向其他地址。
2. 指针常量(指针本身是常量)
#include <iostream>
int main() {
int num = 10;
int* const ptr = #
*ptr = 20; // 可以通过指针修改所指向的内容
// ptr = nullptr; // 错误:不能修改指针本身的值
return 0;
}
这里 const 位于 * 右侧,表示指针本身是常量,初始化后不能再指向其他地址,但可以通过该指针修改所指向的内容。
3. 指向常量的常量指针
#include <iostream>
int main() {
int num = 10;
const int* const ptr = #
// *ptr = 20; // 错误:不能通过指针修改所指向的内容
// ptr = nullptr; // 错误:不能修改指针本身的值
return 0;
}
在这种情况下,指针本身和它所指向的内容都是常量,既不能修改指针指向的地址,也不能通过指针修改所指向的内容。
注意事项
- 理解 const 的位置:const 位于 * 左侧修饰指针指向的内容,位于 * 右侧修饰指针本身。
- 初始化要求:指针常量在定义时必须进行初始化,因为之后不能再修改其指向的地址。
修饰类对象
详细介绍
当 const 修饰类对象时,该对象成为常量对象,只能调用类中的 const 成员函数,不能调用非 const 成员函数,因为非 const 成员函数可能会修改对象的状态。
使用示例
#include <iostream>
class MyClass {
public:
int value;
MyClass(int val) : value(val) {}
void modifyValue() {
value++;
}
int getValue() const {
return value;
}
};
int main() {
const MyClass obj(10);
// obj.modifyValue(); // 错误:不能调用非 const 成员函数
std::cout << "The value of obj is: " << obj.getValue() << std::endl;
return 0;
}
在上述代码中,obj 是 const 类对象,只能调用 getValue() 这个 const 成员函数,不能调用 modifyValue() 非 const 成员函数。
注意事项
- 成员函数调用限制:常量对象只能调用 const 成员函数,确保对象的状态不会被意外修改。
- const 成员函数的实现:const 成员函数内部不能修改对象的成员变量(除非成员变量被声明为 mutable)。
修饰类中成员函数
详细介绍
当 const 修饰类的成员函数时,该函数成为 const 成员函数,表示该函数不会修改对象的状态。在 const 成员函数中,不能修改对象的非 mutable 成员变量。
使用示例
#include <iostream>
class MyClass {
public:
int value;
MyClass(int val) : value(val) {}
int getValue() const {
// value = 20; // 错误:不能在 const 成员函数中修改非 mutable 成员变量
return value;
}
};
int main() {
MyClass obj(10);
std::cout << "The value of obj is: " << obj.getValue() << std::endl;
return 0;
}
在上述代码中,getValue() 是 const 成员函数,不能在函数内部修改 value 成员变量。
注意事项
- this 指针的性质:在 const 成员函数中,this 指针是一个指向常量对象的指针,这意味着不能通过 this 指针修改对象的状态。
- 重载 const 和非 const 成员函数:可以为同一个成员函数提供 const 和非 const 版本,编译器会根据对象是否为常量来选择合适的函数版本。例如:
#include <iostream>
class MyClass {
public:
int value;
MyClass(int val) : value(val) {}
int& getValue() {
return value;
}
const int& getValue() const {
return value;
}
};int main() {
MyClass obj(10);
const MyClass constObj(20);
obj.getValue() = 30; // 调用非 const 版本
std::cout << "The value of obj is: " << obj.getValue() << std::endl;
std::cout << "The value of constObj is: " << constObj.getValue() << std::endl; // 调用 const 版本
return 0;
}
综上所述,const 关键字在不同场景下的使用有着不同的含义和作用,正确使用 const 可以提高程序的安全性和可读性,同时需要注意其使用的限制和要求。