Basic C++ Question

I was looking the GameSession.h file and found the following code in the Global Scope



//Include Files up here
class APlayerController;
class Error;
struct FUniqueNetIdRepl;

//GameSession Class defined bellow


My question is what is the point of this code?
Is it acting as a constructor for those classes? Or is it creating a refrence for the AGameSession(like an #include file)

And my second question is when would I need to use this.

That is just forward declaration.
The guy didn’t want to include the header for those classes just to reference their types.

To expand a bit on what Bruno said,

Forward Declaration is a way to tell the Compiler, “Hey, I’m going to include this class in the future - but you don’t need to worry about it right now.” The benefit to doing that is you don’t have to include another header file just to reference a pointer of some type - which means your header files are smaller and thus get compiled faster. Also if you have some other file that includes this new header, you know it’s not going to pull any a lot of extraneous headers with it.

Alright thanks. I did accually wonder if it was a forward declaration, but when i google it they showed diffrent methods of forward declarations, how would you say you use a forward declartion for seperate class files in particular and is it good practise todo so? I understand the benifits of complie speed and better performance, but are their any risks with it?

Basically i’m asking why you’d ever include a header file over a forward declaration if a forward declaration is always going to be faster…

The “risk” is it doesn’t compile. So, that’s not really a risk at all as you would just do the full include at that point. :slight_smile:

The rule of thumb is if you have a header, and you are including some type of pointer (and not actually using any functionality of that pointer) - just forward declare it.




class MyClass; // Forward Declaration of MyClass. I can now use pointers for MyClass without doing a full include of MyClass.h

// ...

UPROPERTY()
MyClass* SomeClassObject; // Valid. We forward declared this type, so the compiler is okay with it.

void MyFunction(const MyClass* SomeClassObject); // Also valid. We are passing in a parameter of MyClass pointer - but not actually using any functionality.

void CallFoo(const MyClass* SomeClassObject) { SomeClassObject->Foo(); } // NOT Valid. We are calling functionality, which means we need to do a full include so the compiler knows what methods/members MyClass contains.