const 不像其他关键字一样,可以改变代码,而是像一种约束代码的机制,它对声明的东西承诺永不改变,如果你承诺(声明)过,那么请你遵守它,这会对你的代码习惯有所提升。
WARNING⚠️
虽然它只是承诺,存在其他的方法可以打破承诺,但是请不要这样做。
PS:使用指针指向常数内存,然后通过指针对内存中的值进行修改,可以绕过 const 限制
在变量中使用
#include<iostream>
#include<string>
int main()
{
const int a = 5;
a = 2; //不可改变变量值
const int* b = new int;
*b = 2; //不可改变指针指向的内容
b = (int*)&a; //可改变指针指向的对象
int* const c = new int;
*c = 2; //可改变指针指向的内容
c = (int*)&a; //不可改变指针指向的对象
} 对于 const type* 与 type* const 需要仔细区分,两者对限制的对象完全不同
在类和方法中使用
#include<iostream>
#include<string>
class Entity {
int m_X;
mutable int var;
public:
int getX() const
{
var = 2;
return m_X;
}
};在类方法后增加 const 关键字,表示该方法不改变类中任何属性。在 const 关键字声明的函数中需要改变的变量,需要使用 mutable 声明,否则不能改变。
TIP💡
对于 mutable 关键字的使用,不建议在实际的生产代码中使用,尤其是核心代码,毕竟该关键字打破了 const 函数不可改变的性质。使用场景更多在调试代码或者对系统无关紧要的部分
#include<iostream>
#include<string>
class Entity {
int m_X;
public:
int getX() const
{
return m_X;
}
};
void printEntity(const Entity& e)
{
std::cout << e.getX() << std::endl;
}
int main()
{
Entity e;
printEntity(e);
}在传入 const 对象时,只能使用该对象的 const 声明的函数,这是为了保证传入的 const 对象不会被篡改。