# A question on style regarding includes (C++)

## Recommended Posts

[Removed]

Edited by Shaquil

##### Share on other sites

Include what is used directly by the file. So if you use "Engine.h" and "Attack.h" separately from the implied use though using animations, include them.

##### Share on other sites

[Removed]

Edited by Shaquil

##### Share on other sites

No, I mean inside of Engine.cpp you should include headers that do not need to be included from Engine.h.

If you are using references or pointers to class B inside your class declaration for class A, you should forward-declare class B inside A.h and inside A.cpp you should #include "B.h".

L. Spiro

##### Share on other sites

[Removed]

Edited by Shaquil

##### Share on other sites

Additionally, if you are using C++, you should include the C++ headers rather than the C headers.

#include <math.h> #include <cmath>

L. Spiro

##### Share on other sites

So of course, it doesn't *really* matter if you include everything as long as it compiles.

The typical rule of thumb, however, as mentioned above, is to include as little as possible in each place (both in the .h and in the .cpp, meaning that most includes per translation unit should end up in the .cpp file).  This is primarily for 2 reasons, managing compile time and managing dependencies.

Compile time is obvious, because every time you modify something and something else includes it, the latter will need to be compiled again.  And clearly, you'd rather spend more time coding and testing than waiting for things to compile.

Managing dependencies is the other, you can use your includes to document how many dependencies each file has.  As a program gets to be complex, you generally want pieces of your program to be as independent as possible from the other pieces.  Thus, you can evaluate how many dependencies something has by looking at how many includes there are at the top.  (This heuristic works only if you followed the rule above of having as few includes as possible.)

But of course, this rule shouldn't always be followed 100%.  Especially if you're working in a small team or individually.  There's a competing idea of optimizing for coding time ("optimizing for life"), which means that you should do these things only to the point at which you are actually gaining time in the long run.

If you find that having some often-used things in a big "globals.h" include saves you time in the end, and you know you can manage your project's complexity well, then you should consider putting stuff in the global include file to save you typing time in the end.  For instance, if you wrote a run-time debugger, profiler, or even a global game state that you need to query almost everywhere, then just put it in the globals.h to save you typing time in the end.  If you know where your dependencies are, you can always fix up your includes later if you wish.  Especially towards the beginning of a project, when you are still prototyping a lot of different systems.  I think it makes sense to use larger global includes.

Other people may have other wisdom to share in this regard though, as when and where to use global includes is pretty subjective.

##### Share on other sites

My rule of thumb is that every file (including headers) should include precisely what it relies on to compile, to a limit of one level of indirection.

So MainDialog.h would include SpecificWidget.h, but won't bother including BaseWidget.h.

How's that possible? If SpecificWidget inherits BaseWidget, BaseWidget can't be pre-declared - inheritance requires the full declaration, doesn't it?

##### Share on other sites

My rule of thumb is that every file (including headers) should include precisely what it relies on to compile, to a limit of one level of indirection.

So MainDialog.h would include SpecificWidget.h, but won't bother including BaseWidget.h.

How's that possible? If SpecificWidget inherits BaseWidget, BaseWidget can't be pre-declared - inheritance requires the full declaration, doesn't it?

Presumably he means that SpecificWidget.h will include BaseWidget.h. It would be quite a time waster if in MainDialog.h you had to figure out what SpecificWidget needed.

There is, by the way, a trick to ensure that headers include or forward declare everything they need. In SomeClass.cpp, the first include (other than precompiled headers) should be SomeClass.h. Then if that compiles, SomeClass.h has no additional dependencies beyond what it includes or declares itself..

##### Share on other sites

Additionally, if you are using C++, you should include the C++ headers rather than the C headers.
#include <math.h> #include <cmath>

Keep in mind if you do this you should prefix the standard library math functions with std::. According to the standard, if you include <cmath> the declaration go in the std namespace, but may also go into the root namespace. If you include <math.h> the declarations go into the root namespace, but may also go into the std namespace. If you don't want to use the std:: prefix, including <math.h> is preferable, because if you only include <cmath> the non prefixed version is non portable.

##### Share on other sites

A global header is an all-in-one use to have access to all the engine but the compilation time is higher.

Edited by Alundra

##### Share on other sites

It is bad for exactly both of the reasons you just mentioned.

All-in-one is by definition a violation of the single-responsibility principle, which applies not only to objects but to headers, which do have responsibilities of their own.

All-in-one is also by definition a violation of modular design.

Yes it is bad to use a global header, because it is all-in-one and increases compile times.

L. Spiro

##### Share on other sites

My rule of thumb is that every file (including headers) should include precisely what it relies on to compile, to a limit of one level of indirection.

So MainDialog.h would include SpecificWidget.h, but won't bother including BaseWidget.h.

How's that possible? If SpecificWidget inherits BaseWidget, BaseWidget can't be pre-declared - inheritance requires the full declaration, doesn't it?

Presumably he means that SpecificWidget.h will include BaseWidget.h. It would be quite a time waster if in MainDialog.h you had to figure out what SpecificWidget needed.

If he meant, "MainDialog.h includes BaseWidget.h, but not SpecificWidget.h, and MainDialog.cpp would include SpecificWidget.h, which in-turn includes BaseWidget.h", that makes sense. But SpecificWidget.h must include BaseWidget.h if it inherits it, afaik.

My biggest problem is that, by favoring composition, I then have to include alot of minor classes like Rect, which includes Point and Size. I also have to include things like string and vector and map frequently. Those kind of things are included so much, it's ridiculous, and being member-variables, they have to be #included by the header file.

Well, I guess that's one of the benefits of the pImple idiom? Keeping your private composition out of the header file to improve compile times?

Precompiled headers unfortunately have been too buggy when I've used them with the tools I use, that I just ignore pre-compiled headers for now.

I ran HeaderHero the other day on my code - it's annoying to see some of the headers being included 35 thousand times or more.

If only C++'s module system was complete, it'd automagically solve alot of these issues.

Edited by Servant of the Lord

##### Share on other sites

If he meant, "MainDialog.h includes BaseWidget.h, but not SpecificWidget.h, and MainDialog.cpp would include SpecificWidget.h, which in-turn includes BaseWidget.h", that makes sense. But SpecificWidget.h must include BaseWidget.h if it inherits it, afaik.

That's correct. MainDialog only depends on SpecificWidget, so there is no reason why MainDialog should include anything else. If SpecificWidget itself has to include something to work is not MainDialog's problem. For what it's worth, there may not even be a BaseWidget header but that SpecificWidget defines it itself. Either way, MainDialog has no reason to care how exactly SpecificWidget defines itself, only that it is defined.

That is the "one level of indirection"; only include the direct dependencies, and let the dependencies sort out their own dependencies themselves.

##### Share on other sites
I meant to imply that SpecificWidget.h would include the things it needs to compile; assuming inheritance is used, that means SpecificWidget.h is responsible for including BaseWidget.h.

##### Share on other sites
Technically it's possible for SpecificWidget.h not to include BaseWidget.h, as long as everything that includes SpecificWidget.h already includes BaseWidget.h. It's not a good idea, though.

String, vector and map I put in the precompiled header. They work fine for me. Small, commonly used classes like Rect can be put into small headers that don't include anything else. The real problem is when you bring in a whole system for something that's only used in the implementation.

It can be hard to keep on top of it. Every large project I've worked on has had this problem. Pimpl and dependency injection help, and everyone has to be really disciplined about forward declarations and not adding unnecessary headers.

##### Share on other sites

I meant to imply that SpecificWidget.h would include the things it needs to compile; assuming inheritance is used, that means SpecificWidget.h is responsible for including BaseWidget.h.

Ah, that makes sense - I do that too. I thought you meant that your header depths are only ever 1+1 deep, which is nigh impossible.

##### Share on other sites

[Removed]

Edited by Shaquil

##### Share on other sites

You shouldn't have any using-statements at file-scope in a header file at all. Either limit the scope so that the using-statement does not extend into the full scope of the translation unit, or fully qualify any name in the header file.

##### Share on other sites

You shouldn't have any using-statements at file-scope in a header file at all. Either limit the scope so that the using-statement does not extend into the full scope of the translation unit, or fully qualify any name in the header file.

I think I understand why you say that, but could you explain it for me anyway?

##### Share on other sites

Instead of having using-statements in the header file, use the fully qualified name. That is, do this:

#include <vector>
void myFunc(std::vector<myClass> const &);


and not this:

#include <vector>
using namespace std;
void myFunc(vector<myClass> const &);

Edited by Brother Bob

##### Share on other sites

after reading this my program compiles in 1/5th the time it took before

sankyu

##### Share on other sites
You should never put using namespace in a header. It removes the entire point of the namespace and can cause problems for any code that includes it.

I would not have using namespace at all, it makes the code clearer to be specific about where things come from. If you have to do it, put it in the smallest scope possible.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627651
• Total Posts
2978397

• 10
• 12
• 22
• 13
• 33