Whats the difference betweeen including in cpp or header file?

HI THERE! Quick question
Following the course of building escape I wonder what’s the difference between including stuff like
“GameFramework/Actor.h” and “GameFramework/PlayerController.h” in the CPP and not the header file itself. We have “Engine/TriggerVolume.h” and other headers included in the header file, I just cannot wrap my mind around it. :frowning:

Well, there could be other reasons too, but one reason is that you may don’t want to inherit unnecessary headers if you inherit. Included stuff in header file will be inherited forward. This will improve compile times.

1 Like

The general gist of it is stuff you don’t need other classes to include, you will put in the .cpp.

Things that are important to the class, that another class will need can be put in the .h so it includes all necessary files.

However, in saying that, it’s more common place to include everything in the .cpp and do something called forward declaring in the header file. Which is telling the compiler that the thing exists somewhere in the code. As Ezk1 has mentioned it improves compile times, helps avoid code conflicts and strange include chains.

2 Likes

Ooooh! I see, thanks Daniel and Ezk!

Privacy & Terms