Why code sometimes works without forward declaration of classes, but stops working after full rebuild?

Here an example: I have 2 custom classes, say AMyActor and AMyCharacter, and I want to get a reference to AMyCharacter in AMyActor on BeginPlay(); Normally my way to go is to forward declare class AMyCharacter* MyCharRef; in MyActor.h, and #include "MyCharacter.h" in MyActor.cpp.

But sometimes I forget to add class before declaration in .h, and in most cases compiling will result in an error; however, sometimes the code compiles and works as I intend without a forward declaration where it should be.

But then if I for some reason have to rebuild the project by deleting the Binaries folder, now this lack of forward declaration is spotted by the compiler and compilation fails.

So why does it sometimes work without forward declaration?

Precompiled headers, Unreal Build tool and all the other stuff that is underneath the compilation process.

Check the “Why bother at all?” section

1 Like

Thank you, that’s quite insightful.