UE4 C++ Foward Declaration

for class, uenum, and ustruct

Overview

As this documentation mentioned, forward declarations are very useful way to rescue from circular dependency, which may just cause compile error. It also reduced required files included in .h file. However, I was unable to find ways to forward declare uenum and ustruct. So, I'll just write down the approach I found working. Hope this may help anyone struggled with this.

class

Just declare the class as pointer, and then add class before it.

class AUndefinedClass* AUC;

For use of TSubclassOf, you should use this way to declare:

TSubclassOf<class AUndefinedClass> AUC;

In this case, if AUndefinedClass does not exist, it won't compile.

struct

Add struct and pointer make it works.

struct FPickupInfo*;

enum

Put this above UCLASS

enum class EGem : uint8;

and then in your class, just declare it.

Like this:

TestActor.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "TestActor.generated.h"

enum class EGem : uint8;

UCLASS()
class CUSTOMASSETPRAC_API ATestActor : public AActor
{
    GENERATED_BODY()

    UPROPERTY()
    class ATestActor* ATA;  

    EGem GoodGem;

}

Expose to Blueprint...

UPROPERTY are not applicable for the ways to forward declare enum and struct described here. So if you want to expose these variables to blueprints via UPROPERTY, you still need to include the source file which uenum and ustruct are defined to your .h file.

Summary

Now, I can make my .h file as clean as possible and prevent circular dependency issues!

copyright 2021 PufStudio