Candidates for the use of const

Are any of the other functions we’ve written so far candidates for the use of const? If so please suggest which (if any) here.

  • bool IsGameWon() is const-able because we online have to look at a state for win or loss and give a boolean back. No changes necessary here.
  • bool CheckGuessValidity(std::string) is const-able because we only check here some values and don’t have to change them here directly.
  • void Reset() is not const-able because we want to change (reset) our variables here.
7 Likes

Sounds right to me. :slight_smile:

ooold discussion but I don’t think Check Guess Validity would be const worthy as it also process the number of tries (at least he said we would have that do that later)

I think that the fact that CheckGuessValidity() processes the number of tries is what makes it const-able because it would have to utilize MyMaxTries in some capacity and you would want to protect that class defined variable from being directly modified by the function itself.

IsGameWon() works as a const because you’d set the relevant variable to FALSE and you would want it to stay at that default value to avoid accidentally allowing a win without conditions being met.

Reset() seems const-able for the same reason as IsGameWon() in that you would likely want to keep the relevant variable at FALSE so that the game exits by default rather than resetting without an explicit request by the user.

Contrary to what you chaps have said (and I’m probably wrong) I think that ‘IsGameWon’ is not const-able because, and this is where I’m probably wrong, I think it would be necessary to change its value in order for the player to actually win.

The way I see it, if the value can’t be changed from false then when the player wins it won’t be able to return true - i.e. you can be right but you’ll always be wrong. :confused:

I think ‘Reset’ and ‘CheckGuessValidity’ can be const-able though ;).

I’d be glad to hear if and why I’m wrong should anyone have the time.

Edit: Nevermind, I just watched the rest of the video and I realise I got my const locations mixed up. :persevere:

  • I understand your way of thinkin about the ‘IsGameWon’ state. What you mean with true and false is the return value of the function from IsGameWon as Boolean. If you does return true; or return false; you don’t make any changes in the function. You only check some variables and states to determine whether it is won (return true) or not (return false). The changes you make, if the game is won, don’t apply in the IsGameWon-Function.
    I hope it helped your understanding. :slight_smile:
  • It is not possible to make Reset be const-able, because “reset” means to change a state or variable to its previous (or start) content.

Edit: Good, but I let my text for others here. :smiley:

2 Likes

This is all very interesting, I never considered const to work for any of the above mentioned. I was really close to not posting at all because I felt that only max tries would be a constant

That makes sense to me, and more sense then what I was thinking. Essentially, in other words, with the exception of void reset(), everything else could be a constant because it does not need to be changed or change anything during run time, right?

I think I understand thanks to the discussions that GetMaxTries(), GetCurrentTry(), and IsGameWon() only pass along information from elsewhere without changing that information themselves.

I don’t understand how Reset() and CheckGuessValidity() are not const. Won’t their values be changed only externally as well?

const at the end of a member function, e.g int FBullCowGame::GetMaxTries() const; means that the function doesn’t modify any data that belongs to the class. FBullCowGame::Reset modifies the data members, IsGameWon, MyCurrentTry, and MyHiddenWord.

2 Likes

Thank you so much. I was only thinking of how the function might modify itself, not the entire class. That makes perfect sense now.

I think that CheckGuessValidity is not const-able because it must change MyCurrentTry.
just a though

Urhm I would suggest the method IsGameWon() to be a candidate for the use of const. Maybe in the future when we define this method we could possible change something we would’nt or should’nt be allowed to change. I’m not sure if that makes any sense?

:slight_smile:

So a function is a member to a class if it is defined in that particular class ?

Only if you also qualify it by the class name. e.g.

class FBullCowGame
{
public:
    int GetMaxTries();
private:
    int MaxTries;
};
//non-member function
int GetMaxTries()
{
    return 5;
}

int FBullCowGame::GetMaxTries()
{
    return MaxTries;
}

int main()
{
    FBullCowGame BCGame;
    BCGame.GetMaxTries(); //call member function
    GetMaxTries(); //call non-member function
}
1 Like

Thank you so much for that explanation. :grinning:

The only ones I can see that should be const right now would be GetMaxTries() and possible GetCurrentTries() . Later I can us removing const from GetMaxTries() based on game settings for example: Easy 8 tries, Med 5, and Hard 3. I have GetCurrentTries() as a const because we always start with try 1, and I have another var in the loop that increments.
example:
void PlayGame()
{

int MaxTries = BCGame.GetMaxTries();
int CurrentTry = BCGame.CurrentTry();

std::cout << "you have a max of: " << MaxTries << " to figure it out...\n";
for (int i = 0; i < MaxTries; i++)
{
	std::string Guess = GetGuess();
	std::cout <<"try " << CurrentTry << " So you guessed " << Guess << " Right? \n";
	std::cout << std::endl;
	CurrentTry++;
}

The way i understand it (If I’m right that is haha) . Any other getters can become const-able depending on it’s functionality within the game. But of course, you would need const’s to shield from changing values that should NOT be modified, and changes that could interrupt the game’s processing. And as for void, well, it’s nothing, so their would be no need to have it const worthy for the time being.

All the functions that are Getting values are const-able since you would use a setter function to change the values for those member variables that require it. For example, MyCurrentTry will be incremented later in the course and need to be set through a dedicated function or single line in a loop.

With this logic, Reset() would be non-const-able.

Privacy & Terms