My philosophy is that in well-written code, the header files should include all the other header files on which they depend. My reasoning is that you should not include the header file and get a compiler error for this. Therefore, each header file must (after #ifdef or #pragma once enable protection ) include all other headers on which it depends.
To informally check what you remember, to include the correct headers in the header files, * .cpp files must # include a minimal set of header files that should work. Therefore, if there are separate header files for A , B , C and D , and your cpp file uses class D , then it should include only Dh . Compiler errors should not occur because Dh #include Ah and Ch , Ch includes Bh and Ah and Bh include the SFML header (whatever it is). Ch and Dh may include the SFML header if this seems appropriate, but it is not necessary if you can be sure that the dependencies ( Bh and Ah ) have already included it.
The way Visual C ++ makes "precompiled headers" twists this logic. This requires including "StdAfx.h" as the very first header file, which forces many developers to simply put all #include for the entire project in StdAfx.h and not use #include in any of the other header files. I do not recommend this. Or they will place all external dependencies in StdAfx.h (for example, windows.h, formatting headers) and # include local dependencies elsewhere so that changing one header file does not necessarily lead to a rebuild of the entire project.
As I write my code, most of my CPP files include StdAfx.h and the corresponding .H file. So A.cpp includes StdAfx.h and Ah, B.cpp includes StdAfx.h and Bh, etc. The only other #include placed in the cpp file is the "internal" dependencies that do not appear in the header file. For example, if class A calls printf() , then A.cpp (not Ah ) will be #include <stdio.h> , because Ah is independent of stdio.h .
If you follow these rules, then the order in which you #include header does not matter (if you do not use precompiled headers: then the precompiled header will be the first in every cpp file, but you do not need to be included from the header files).
Qwertie
source share