Header only c++ classes

I am vetran modern c++ developer new to unreal Engine.

What is the guidance best practice regards header only classes, as i prefer this, don’t like switching back and forth between cpp/header if i dont have to.

The Unreal engine docs mentions special UCLASS specifier for ctor in header , something like “custom constructor” cant remember exact name.

But besides this little inconvenience are there any reasons why headeronly is ill advised for unreal engine.

Basically meaning i genrare my unreal classes with editor izard, but the cpp will be empty, with everything implemented in header. Pros/Cons Please


A couple of reasons I almost always split mine into .h and .cpp files:

  1. Unreal relies on “include what you use,” If you refer to a class in your declaration of a variable or function, then you can simply forward declare that class in your header file. But the moment you reference that class in a function definition, you need to #include the file that declares that class. If you put all the function definitions in your header files, and therefore put all those #includes in your header files, you will end up with circular #includes, which creates problems. Declaring the functions and variables in your .h and then defining the functions in your .cpp allows you to just put forward declarations in your .h files and put the includes in your .cpp files, so you can avoid circular #includes.

  2. Readability. My .cpp files are sometimes thousands of lines long, and sometimes a class has over a hundred functions. Putting the declarations and definitions in a single file would be clumsy for me, but YMMV on this issue.

#2 is subjective, but #1 will definitely create problems for your as you create and use more classes.

Thanks for the tips much appreciated

Using .h/.cpp files properly is essential - you will reach a point where you can’t compile otherwise. In games, you often have lots of classes, all regularly including and dealing with other classes.

All that aside, your compiles times will be obscene if you put everything in headers. You should put as much code as you feasibly can in the cpp. In addition, you also won’t be able to benefit from things like Live Coding which dramatically improves iteration times when editing cpp files.

I strongly advise studying the content samples, and following the UE coding standard.

Thanks for the feedback, header only is ruled out for me.

You can use hpp files, but depending on what you do, you can get the Unreal Build Tool confused.