[prog] [C/C++] what to put in header files.

Kathryn Hogg kjh at flyballdogs.com
Thu Sep 19 09:26:05 EST 2002


> One rule that really helps with compiling huge projects is that each
> include file should include as few other files as possible.  If class
> Foo uses class Bar, it's much better to include Bar.h in Foo.cpp rather
> than in Foo.h.

May I add something to this advice?  If Foo.h uses anything from class Bar,
then Foo.h should include Bar.h.  Not only does this make it easier users of
class Foo since they only need to include Foo.h but it may be required for
C++ template instantiation on some systems.  Of course, I always guard
multiple inclusion of dependent header files within files.

 > Those things you often see at the beginning and end of an include file:
>
> #ifndef FOO_H
> #define FOO_H
> [ ... body of file ... ]
> #endif /* FOO_H */

I always use some sort of organization tag like CES_FOO_H since I may use
code developed elsewhere that uses FOO_H.

-- 
Kathryn





More information about the Programming mailing list