Why is class keyword before pointer in unreal puzzle game?


In the unreal puzzle game, I see statements like this in the PuzzleBlock.h:

class UMaterialInstance* OrangeMaterial;
class APuzzleBlockGrid* OwningGrid;

or even functions like:

class USceneComponent* GetDummyRoot() const { return DummyRoot; }
class UStaticMeshComponent* GetBlockMesh() const { return BlockMesh; }

It makes sense to me without the class keywords in the code. When the class keyword is added, I do not understand why. Any explanation would be greatly appreciated. Thanks in advance.

I had never seen this before I began using UE4 either.

Essentially, it’s just doing an inline forward declaration. It’s the same as putting

class USceneComponent;

at the top of the file, and then omitting the class keyword in front of the variable/function declaration.

Personally I prefer the above approach since it’s more succinct (if you use the class multiple times in the file, using the class keyword each time is redundant; but prefixing it only to the first use makes the code inconsistent).

For clarity, a forward declaration is a way to reference a type for restricted use (usually declaring a pointer or reference to it), without needing to include the header file. This is further confused in UE4 by the tendency not to include headers directly anyway, but rely on things being included by the module precompiled header (a practice I’m even less keen on).

Thanks for the response! It really helped!