Relative Content

Tag Archive for include

Including local headers first

So I read up on the ordering of your includes, and this guy suggested you include your local header first so as to make sure it doesn’t have prerequisites. Ok, I get that. I’m on board. The whole compartmentalization thing is good.

Ensuring that headers are explicitly included in CPP file

I think it’s generally good practice to #include the header for any types used in a CPP file, regardless of what is already included via the HPP file. So I might #include <string> in both my HPP and CPP, for example, even though I could still compile if I skipped it in the CPP. This way I don’t have to worry about whether my HPP used a forward declaration or not.

Single complex or multiple simple autoload functions

Using the spl_autoload_register(), should I use a single autoload function that contains all the logic to determine where the include files are or should I break each include grouping into it’s own function with it’s own logic to include the files for the called function? As the places where include files may reside expands so too will the logic of a single function. If I break it into multiple functions I can add functions as new groupings are added, but the functions will be copy/pastes of each other with minor alterations.

cpp and h/hpp #include: “why” question

Why does the source include a header and not also the other way around? I googled it but only found questions regarding the use of header files, how to include them but nowhere to say why it is like it is.
If the header is merely the declaration, how does the compiler know the definition only from it?
For example: take foo.cpp, bar.h, bar.cpp. This is what everybody does:
in foo.cpp:
#include "bar.h"
but the bar.cpp is not included neither in the bar.h or foo.cpp. That’s why I deem logical that bar.cpp be included in bar.h and so, indirectly in foo.cpp.