Other candidates for const

Hello all!

So in response to the challenge that was being posed of which other variables might need a const I have this to say:

The GetMaxTries() and the GetCurrentTry() are the only one within the class I deem suitable for const. at this moment in time.
They are integers, numbers, that need to be set.

The other ones, are void and boolean. Void returns nothing, so there is nothing to const. Boolean is either false or true, so again no number to const.

Let me know if you think I am right.

Regards, Kim

1 Like

Hey Kim.

The point on marking methods as const is to let know the compiler that you are not going to update class members in that method.
This is like a protection of accidentally messing with its data.
For example:

class FBullCowGame {
int GetMaxTries() const;
private:
int MaxTries;
}

int FBullCowGame::GetMaxTries() const
{
MaxTries = 10; <-------- this is not allowed
return MaxTries;
}

Max tries is a member of a class, and by declaring’ GetMaxTries as const, you can not change ANY of the class member’s state.

So i would say that IsGameWon() and CheckGuessValidity() should also be marked as const.
Also by the name of a method it should be clear if method is going to change class member’s state.
IsGameWon() does not sound like to change anything in the class.

2 Likes

Hei Maxim, thanks for your reply in this topic. It allowed me to get better the “Why” of the const! : ]

Privacy & Terms