you don’t want to change the value.
int main(){
const int MAX_AGE = 90;
// MAX_AGE =2 ; not work
const int* a = new int; // you can't change the content of a
int const* a = new int; // you can't change the content of a
int* const a = new int; // you can't reassign the actual pointer itself
class Entity{
private:
int m_X, m_Y;
int* p_X;
mutable int var;
public:
int GetX() const // it means read-only.
{
// m_X = 2; // dosn't work.
var = 2; // it works, because it is mutable.
return m_X;
}
int SetX(int x) const // it means read-only.
{
// m_X = x; // dosn't work.
}
const int* const GetX() const
{
return m_X;
}
// first const: returning a pointer which can't be modified.
// second const: the content of pointer can't be modified.
// third const: this function can't modify the actual Entity class.
};
void PrintEntity (const Entity & e)
{
// we can't modify the Entity.
std::cout << e.GetX() << std::endl;
// If there is no const in GetX(), then can't use e.GetX().
// Because the value could be changed.
}