I should have one main include file which includes all the other needed files |
Not really.
Ideally a .cpp file should only #include what it needs. For example, if main.cpp uses GameEngine and Entity, but no other classes, then it should only #include "gameengine.h" and "entity.h".
"package" headers (headers which do nothing but #include common groups of headers) are another option, but overusing them, or making the package too big, defeats much of the point of splitting up headers in the first place. Speaking of, that's one topic I didn't mention in that article.
EDIT: that said.. package headers are often very useful. If you find yourself #including the same 3 header files in every cpp file.. .then yeah you might want to put those 3 headers in a package.
in the case of a circular dependency I use forward declaring to declare a class outside of its header. |
You should forward declare IN the header, BEFORE the class, and ONLY for classes that need to be forward declared.
The idea is to make note of the
dependencies that each class has, then either forward declare (if possible) or #include the dependency in the header. If all the dependencies are covered, then there's no problem.
So if I have my GameEngine.h, which contains the GameEngine class, which contains a pointer to my RenderEngine, FileParser and EventHandler, |
Then those 3 classes should be forward declared in GameEngine.h, before the GameEngine class
each of which contains a pointer to a GameEngine class, do I have to forward declare each class inside all the header files? |
If you want to do it the "right way", then yes. GameEngine is a dependency for each of those classes, and thusly must be forward declared.
EDIT 2: If you want, you can post one of your headers in here and I can see if you're doing it right. Try to pick a header that isn't super huge though.
Or you can pastebin it and post the link.