I have a question on how to use and define Enums or Structs without a class or out of a class file.
Many examples show Enums and Structs in a header file (*.h) of a class. As this exaample, declare an Enum before a Class:
But I don’t know if it’s possible to define Enums or Structs in a header file without a class, as in Java. As this example:
I tried it and it just requires making an include, and runs ok.
Is there any objection to create my Enums or Structs in this way? Is it necessary to add in “MyEnum.h” the line #include “MyEnum.generated.h”?
Thx for helping me.
Yes, *.generated.h is generated header file required for reflection system to work, if you use UENUM and USTRUCT (which is not required to add btw, but needed if you want to use those in blueprints) you need to include generated header in that file so UHT can add extra stuff needed for reflection.
In technical state point C++ does not divide files by types, for it header files and cpp files are same thing and #include is pasting one file to another. Because C and C++ compilers compile files individually, you need to declere things in sperete file (header file) so you don’t compile things twice or more but still make compiler aware of classes. So generally it does not matter for compiler where you place things, but for higene reason declaring should be in header file, definitions in cpp files, also UHT, reponcible for generating reflection code scans only header files.
So in short it’s ok to place Enums and Struct in seperate header file.
Also don’t forget about #pragma once, it prevents inclusion of file twice and prevent compiler thinking theres 2 declarations of same things (because double include) which would throw you a error. As i said main function of #include is paste file in file
Thank you very much for the explanation.
For me is more organized using Enums and Structs in separate header files.
What advantage does not do this?