0
\$\begingroup\$

What happens if I extend a uclass (or UObject) without the UClass() macro?

Will it still be a UClass() and managed by the garbage collector?

I'm just curious.

Update

I managed to extend ULineBatchComponent without UCLASS nor GENERATED_UCLASS_BODY(), it compiled, did not complain, and I could instantiate it.

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

UCLASS evaluates to a marker used by the Unreal Header tool to inject various bits of code, typedefs, attributes, and other bookkeeping information related to Unreal's reflection (and thus garbage collection) systems.

Anything that has UCLASS should also have GENERATED_UCLASS_BODY in the body of the class. Without both of those, your code shouldn't compile or link.

Failure to include UCLASS causes UHT to skip generation of the YourFile.generated.h header, which you should be including, and if you're not including that header you'll get compilation errors within the evaluation of GENERATED_UCLASS_BODY.

It may be possible to hack up the source file to get it through the compiler somehow (I stopped trying after a few basic attempts) but at that point you won't be able to instantiate or completely use the class because you won't have the related type info metadata Unreal's object system requires for things like NewObject to function.

tl;dr: bad things, don't do it.

\$\endgroup\$
3
  • \$\begingroup\$ Thx! I could extend ULineBatchComponent without UCLASS as I explained in my edit. \$\endgroup\$
    – arthur.sw
    Commented Mar 24, 2016 at 9:27
  • \$\begingroup\$ That's likely going to cause you trouble. You can instantiate it without Unreal's object system but you're going to be asking for bugs and memory leaks in the future. \$\endgroup\$
    – user1430
    Commented Mar 24, 2016 at 13:52
  • \$\begingroup\$ Yes, I changed my code after your answer, thank you! \$\endgroup\$
    – arthur.sw
    Commented Mar 24, 2016 at 13:59
2
\$\begingroup\$

You won't be able to instantiate the class without it because the UCLASS() macro above the class declaration creates the global UClass instance (a ptr to which is returned when you do GetClass() on an instance of the UObject) describing that class' metadata, which is required for spawning it (either via the editor/dynamically or via NewObject<>)

That is, if UHT doesn't catch you using UPROPERTY/UFUNCTION inside the body of something that isnt declared as a UCLASS or USTRUCT beforehand, which will make it error on the preprocessing stage.

\$\endgroup\$
1
  • \$\begingroup\$ Thx! I could extend ULineBatchComponent without UCLASS as I explained in my edit. \$\endgroup\$
    – arthur.sw
    Commented Mar 24, 2016 at 9:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .