Jump to content
  • Advertisement
Sign in to follow this  
Liuqahs15

A question on style regarding includes (C++)

This topic is 1970 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement

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.

 

Also, prefer forward declarations instead of including class headers where possible.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!