Declare UCLASS() classes in source file

Hello guys, I have another UCLASS related question.

Yesterday, i knew from someone that nested UCLASS() are not supported but now i’m facing another situtation and I want to know if it’s supported by the engine or not.

Can we declare UCLASS() classes in the source file? I saw Standart C++ classes declared in source files within the engine, but what about UCLASS()?

My code don’t compile so i’m guessing no it’s not supported, but if I could have a clear answer.

My goal is to “shield” a class and it’s subclasses form the rest of the project because it’s only use and relevant for one class.

Thanks for help

gamer08.

Sorry to say, but that’s also not supported. UHT (the Unreal Header Tool) will only scan header files looking for classes to generate reflection information for.

The only “shielding” of classes we support is at the module level, and this is done by putting your header file inside the “Private” folder for its module - this makes it inaccessible outside of that module.

Maybe i explain for you want UCLASS() really so you can figure that yourself.

UCLASS() is a marker for program called UnrealHeaderTool, that tool is executed before compiling, it generates code which registers all classes, varables, function etc. for so called reflection system, which let engine see and identify those things, because normal C++ does not have such feature (all function and varables are turn in to memoery addresses inside binery code which code it self can’t destingush if it’s not coded to do so by regisstring them and thats what reflection system is for). UCLASS() need can be only in header file (because UHT only checks those) only before declaration of class, this is only way it can work, UHT expects to see class deceleration below UCLASS() to read information about class, same with other macros, UHT expects varable declaration below UPROPERTY() and function below UFUNCTION(). Also all U macros are limited to what reflection system recognize, which does not support all C++ types, so those macros can’t be used on everything. UCLASS() in specific only is also limited to UObject classes, as reflection system is made so all classes inherence from main root Object class.

Also it is importent to know that even with fancy UHT you still coding standard C++ and you don’t need use those macros at all (but UCLASS() is required for all UObject classes, engine needs to know about all UObjects), those once simply won’t be visible by reflection system and engine won’t see them and engine has low of Structs and non-UObject classes like that, but you need to keep in mind you won’t be able to use those in some engine features (most notably some delegates types and timers won’t accept nonreflected functions) also those stuff won’t be visible by garbage collection so you will need to take care of cleaning them from memory. If you using something unsupported by reflection system (but you should use supported things if possible) you are force to not place U macros on those.

Thanks for both answer Jamie & it’s appreciate.

What you says Jamie is interesting:

"The only “shielding” of classes we support is at the module level, and this is done by putting your header file inside the “Private” folder for its module - this makes it inaccessible outside of that module. "

I’m not sure to follow you on that, can you elaborate a bit please?

Thanks for help

gamer08

Header files in Private won’t get extern linking so it can’t be called them from outside of module (dll file)

Not strictly true, since the external linking is something you need to add manually using the _API macro for your module.

Basically, files in the Private folder can’t be used in a #include outside of that module, since they don’t get added to other modules include paths.

I have a more detailed explanation of this here: Explanation of Source Code folder structure? - C++ - Epic Developer Community Forums

Thanks again both of you. Another new thing learned related to UE4 !