Have you ever been hunting that weird compilation error cause by C++ header files hell, where nothing seems compiling anymore if you touch an include directive?
I have, it’s not funny, it’s tedious and annoying.
I “strictly” follow three rules:
- One class per header file
- No inline method in the header file
- Each header file compiles independently
One class per header file.
It promotes a very Java-like style and helps quickly guessing the content of the header file, ehm… the class with the same name.
Exceptions: very small and closely coupled classes or interfaces.
No inline method.
Why do you want to force a method inline? Performance? Give a look at what Herb Sutter says about it.
You can always inline a critical method in an inner loop if the profiler begs for it. No inline method promotes less coupling between header files, faster compilation times, less chances to find your self in Header Files Hell. The compiler will likely inline all methods it finds suitable when Whole Program Optimization is turned on.
Exceptions: pure accessor methods, but I still implement them in the C++.
Each header file compiles independently.
If the header file is the only include directive in a translation unit, does it compile correctly? If not, I include in the header file everything that is necessary in order to compile: I never rely on the correct order of include directives in order to compile correctly. It’s extremely error-prone, cumbersome, hard to control and takes straight to Header Files Hell. Avoid it.