Const is generally used to state that something will not be changed or modified. However, this is dependant on where const is placed. For example if you place the const keyword after a member/class function declaration, you will not be allowed to modify any member variables within that function.
int GetMaxTries() const;
int GetMaxTries() const
{
return maxTries;
}
The above function and function declaration will not produce any compiler errors as it doesn't modify any of the class's member variables. However, the below example would, because even with the const someone has tried to modify the maxTries variable. The const keyword in this situation acts as a guard so that you or anyone else working on the codebase doesn't accidentally modify any member variables within a specific function.
int GetMaxTries() const;
int GetMaxTries() const
{
maxTries += 12;
return maxTries;
}
No comments:
Post a Comment