#include and ::

Hello Forum,

I’ve been following along and I’m really starting to wonder why it’s necessary to write “int32 FBullCowGame::” before typing out the function/method that you’re after. It seems to me that similar to how “std::” allows you to access a folder, “FBullCowGame::” allows you to access “FBullCowGame.h”. What I don’t understand is if “#include” means you can treat your header file as being pasted at the top of the associated CPP, why do I need to prefix its function with “FBullCowGame::” when I call them?

My worry as I work through the course is that by the end of it, I’m not going to know when to use which method to call functions. It seems that there are so many. Sometimes we use “using”, sometimes we use “::”, sometimes we use “.”. As well, sometimes we need to type out “int32” or “bool” before a function and other times we only type the name. Will this all become clear with repetition?

Thank you for any solutions or help you offer, it’s very much appreciated.

So std is a namespace which allows you isolate your code, which is extremely useful when writing a library. It’s like giving your code an area code (as in for a phone number).
When you write int32 FBullCowGame::GetMaxTries() const { return MyMaxTries; } that means you are defining a function that belongs to the class FBullCowGame, where int32 is the return type.

Because you would still need to say it belongs to that class otherwise you would just be creating a function outside of that class, you could write this in the header file and would still need to qualify it with FBullCowGame, simplified version of the class in the same file

class FBullCowGame
{
public:
    int32 GetMaxTries() const;
private:
    int32 MyMaxTries;
};

int32 FBullCowGame::GetMaxTries() const
{
    return MyMaxTries;
}

int32 GetMaxTries() const // error const at the end of a non-member function
{
    return MyMaxTries; //error MyMaxTries is undefined
}

The instance in where you don’t have to qualify it with the class name is if you are writing it within the class itself

class FBullCowGame
{
public:
    int32 GetMaxTries() const { return MyMaxTries; }
private:
    int32 MyMaxTries;
};

using is for type aliases like using int32 = int which is just saying int32 is the same as if we’re saying int. using namespace x says to put essentially break the isolation of x and put it into the global space so you don’t have to qualify it by x::

:: is the scope operator, and says the right hand side belongs to the left hand side i.e. FBullCowGame::GetMaxTries, GetMaxTries belongs to FBullCowGame

. is like :: except the left hand side is an object i.e.

FBullCowGame BCGame; //create an FBullCowGame object called BCGame
BCGame.GetMaxTries(); //call GetMaxTies

That’s to denote the type when declaring/defining something, you don’t use it when you’re just using the function/variable because the type is already defined and adding it would just define a new function/variable

//declare and define a function that takes an int and returns an int
int square(int n) { return n * n; }
int main()
{
    int i; // declare variable i
    i = square(i); //pass i into square and assign the returned value to i
    return 0;
}

Yes. Playing around and tinkering around with your code will also help.

Hi Dan,

Thanks again, I think I understand now but I have another question concerning a line like this:

My understanding here is that this line is calling “CheckGuessValidity(FString)” (a member) from within “FBullCowGame” (a class) and giving it a handle called “Guess”. This handle allows us to access information from that string such as its length. This line is also a “const” because it does not change any values within its class.

What I don’t understand is how “EGuessStatus” can be at the front of the line. Wouldn’t this mean that we’re creating a variable called “FBullCowGame”?

Thank you for your time. I feel like I’m close to understanding this.

That isn’t calling anything. That’s defining the CheckGuessValidity function.

EGuessStatus at the front is the return type of the function. Going through that step by step

EGuessStatus - return type
FBullCowGame::CheckGuessValidity - name of function in the FBullCowGame class
(FString Guess) - that function takes 1 parameter which is of type FString
const - doesn’t change any of the class’ data.

Privacy & Terms