Questions about the structs

I did it a little differently and it works just as well.

So in the header file I declared

void GetBullCows(const FString& Guess, FBullCowCount& Count) const;

Then in the ProcessGuess function I wrote:

FBullCowCount Count; 
GetBullCows(Guess, Count);
PrintLine(TEXT("You have %i Bulls and %i Cows"), Count.Bulls, Count.Cows);

And finally here is the GetBullCows function:

void UBullCowCartridge::GetBullCows(const FString& Guess, FBullCowCount& Count) const
{

    for (int32 GuessIndex = 0; GuessIndex < Guess.Len(); GuessIndex++)
    {
        if (Guess[GuessIndex] == HiddenWord[GuessIndex])
        {
           Count.Bulls++;
           continue;
        }
        for (int32 HiddenIndex = 0; HiddenIndex < HiddenWord.Len(); HiddenIndex++)
        {
            if (Guess[GuessIndex] == HiddenWord[HiddenIndex])
            {
                Count.Cows++;
                break;
            }
        }
        
    }
}

I’m a beginner at C++ so i’m wondering if writing the code this way that I did is discouraged. Since I’m a noob I didn’t really think about that you can put the class (the struct) before you write the UBullCowCartridge class to write the function.

I thought about what would be most logical from the way we’ve done things before. So it made the most sense to me to just pass the struct by reference (since its integer values are going to change) and then declare the FBullCowCount as Count in the ProcessGuess function and send that to the GetBullCows function and then the only thing I would have to change in that code is the incrementation of the bulls and cows. To me this is more intuitive than the way the teacher did it because then I don’t have to return Count back to the ProcessGuess function and I don’t have to instance the struct again as Score and set it equal to the GetBullsCow(Guess).

However, since I am very much a beginner of not just C++ but programming in general and the teacher is a professional and knows his stuff I wonder if the way he does it is the preferred or “correct” way, and why in that case?

The only thing i can think of is that the struct has to be allocated in memory each time it runs through the ProcessGuess function.

That in itself isn’t too bad in this case and i believe it is perfectly fine to be written the way you have written it.

If it was a larger struct or you wanted to be slightly more optimized with it i believe adding the struct as a member to the class and just referencing from it, may be slightly better than creating a new struct each time the processguess function is called

Okay, thank you very much.

To note, that’s the return type.


Recommended practice is to prefer returning values rather than out params.
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-out

The struct is just two ints so would most likely be using registers and thus not require allocating any memory. That wouldn’t be the case with them as member variables as that would need to allocate memory in order to store them in the class.

When i went through this section instead of having a GetBullCows function i had a PrintBullsAndCows function which took in the guess and counted the bulls and cows but also contained the call to Printline() so it didnt need to return anything

Solution for the error :“this declaration has no storage class or type specifier” that pops while declaring a struct before a class in the BullCowCartridge header file

Actually there is an Intellisense bug that pops when declaring a struct before the class in the “BullCowCartridge.h” file.
When declaring a struct for the first time in the “BullCowCartridge.h” header file VSCode will give an error as:“this declaration has no storage class or type specifier” because the GENERATED_BODY() has to be the 8th line of command in the header file but declaring a struct changes this fact…hence the error.
The only solution here is to save and compile the code as technically there is no syntax error …its just a
IntelliSense Bug.After Compiling the code this error line does not appear.

Hope this helps!!

1 Like

So I guess I kind of understand a little bit why returning values is to be preferred over out params but then why was the function originally written to use out params? Why use return types for a struct that contains two ints but out params for two individual ints?

Because Unreal uses them a lot so it’s to get a familiarity with them.

https://stlab.cc/tips/stop-using-out-arguments.html

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

Privacy & Terms