I found myself confused by the scoping of our struct in this lecture. As I am interpreting the solution shown, we are scoping the struct definition/declaration of FBullCowCount to the global namespace.
First, am I right, or is there some magic here I am not picking up on?
Second, if I am right, would it be better practice to scope FBullCowCount internal to UBullCowCartridge? This was my intuition and how I implemented it before watching the lecture through.
Or, alternatively, is scoping this struct to global ok because we expect no other implementation file or header file to ever #include BullCowCartridge.h and so don’t have to worry about naming conflicts?
If we were worried about naming conflicts, and didn’t want to tie our struct definition to our class, would this be an appropriate place to assign this struct a custom namespace?
For practice I implemented the following three architectures (mostly to prove to myself they were all valid and wouldn’t generate a compile error):
- FBullCowCount declared/defined in the header global namespace (outside of the class), and used to instantiate a local variable (same as lecture solution).
- FBullCowCount declared in the header, inside class UBullCowCartridge, and defined in the implementation file using scope resolution. Then used to instantiate a local variable for computation.
- FBullCowCount declared/defined in the header inside class UBullCowCartridge, then a separate member variable of type FBullCowCount declared inside the header. This member variable was then used in the calculations.
Since all three work, there has to be a “preferred” style in C++, where the struct definition has appropriate scope. My vote is 3, but I would be very interested in other people’s thoughts.