I was watching this seminar on C++ best practices, and the speaker gave this code example:
struct Data{
int x;
int y;
bool operator==(Data &rhs){
return x == rhs.x && y == rhs.y;
}
};
He then asked what was missing in this code. As a newbie I thought that nothing was missing, but then he pointed out that 2 const
keywords were missing, like so:
struct Data{
int x;
int y;
bool operator==(const Data &rhs) const{
return x == rhs.x && y == rhs.y;
}
};
Now I think this is like a promise not to modify the object. But can someone explain why these const
keywords are necessary?