Const member function lecture (section 3 ep 64)

as u can see in the picture Ben said i could move this to a const function but there is EndGame() function in it which changes the value of bGameOver and that doesn’t make sense to me because it was said in the beginning of the lecture that i can’t use a non const function inside a const function?

Well it’s slightly misleading but he meant the checking can be made into a const member function. e.g.

if (IsGuessValid(Guess))
{
   // proceed
}
1 Like

so its fine to have endgame function inside the const function you just typed?

Sorry that was a little unclear but no. It would be rather surprising if a function called “IsGuessValid” called EndGame(), it should only be checking if the input is valid e.g.

bool UBullCowCartridge::IsGuessValid(const FString& Guess) const
{ 
    if (Guess.Len() != HiddenWord.Len())
    {
        PrintLine(TEXT("The hidden word is %i letters long."), HiddenWord.Len());
        PrintLine(TEXT("Sorry, try guessing again! \nYou have %i lives remaining."), Lives);
        return false;
    }

    if (!IsIsogram(Guess))
    {
        PrintLine(TEXT("No repeating letters, guess again!"));
        return false;
    }
    return true;
}
1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms