Reason [1] Faster compilation time
Not in my projects: source files (CPP) include only the headers (HPP) they need. Therefore, when I need to recompile only one CPP due to a tiny change, I have ten times the number of files that are not recompiled.
Perhaps you should split your project into more logical sources / headers: to modify a class A implementation, you do NOT need to recompile class B, C, D, E implementations, etc.
Reason [2] He avoids circular dependencies
Circular dependencies in code?
Sorry, but I still have a problem that is a real problem: let them say that A depends on B, and B depends on A:
struct A { B * b ; void doSomethingWithB() ; } ; struct B { A * a ; void doSomethingWithA() ; } ; void A::doSomethingWithB() { } void B::doSomethingWithA() { }
A good way to solve the problem is to split this source into at least one source / header for each class (similar to the Java method, but with one source and one header for each class):
.
.
.
// B.cpp
So there is no dependency problem and still fast compilation time.
Did I miss something?
When working on real-world projects
in a real project, cpp files usually include random headers until you can figure out who depends on whom
Sure. But if you have time to reorganize these files to create your “single CPP”, you have time to clear these headers. My rules for headings:
- break the header to make them as modular as possible
- Never include headers you don't need.
- If you need a character, forward-declare it
- only if the above failed, include the header
In any case, all headers should be self-sufficient, which means:
- The header includes all the necessary headers (and only the necessary headers - see above).
- an empty CPP file including one header should compile without having to include anything else
This will fix ordering issues and circular dependencies.
Is compilation time a problem? Then...
If compile time is really a problem, I would consider either:
Conclusion
What you do does not put everything in the headers.
Basically you include all your files in one and only one final source.
Perhaps you are winning in terms of compiling the complete project.
But when compiling for one small change, you will always lose.
When coding, I know that I often compile small changes (if only so that the compiler checks my code), and then, for the last time, make a complete change to the project.
I would have lost a lot of time if my project had been organized.