Cy said:
Chris, IMHO this is truly a terrible idea. There is nothing wrong with
including headers within headers. But your Library_Headers.h is unduly
obscure (since a third party wouldn't be familiar with it) and forces a
complete recompile of all your source code whenever you change it. Worse
yet, if you decide to reuse your header in a different context years from
now you will have no idea what Library_Headers.h contained.
Thanks for the comments. I think that we are talking at cross purposes.
Perhaps I should have worded my posting more clearly. The main purpose of
Library_Headers.h is to include headers from the Standard Template Library
(STL). I think therefore that there is nothing obscure, and its contents
will be familiar to any third party (and to me!) in several years time. If
you were thinking that I was including all my own headers in
Library_Headers.h, then I agree that this would indeed be a very bad idea.
Apart from the recompiles you mention, it would make it very difficult to
see which parts of my code depended on which headers; everything would be
visible to everything.
On the point about including headers in headers, I prefer to avoid it as its
easier to understand the dependencies that way. The only place that I do it
in my project is where i have a class that hides its implementation from
clients by having a separate 'protocol' class from which it inherits. The
protocol class has to have its own header file, but the class with the
hidden implementation can't compile unless the protocol class is visible to
it. It therefore makes sense to include the header for the protocol class
from within the header file for the class with the hidden implementation.
Although I agree that there is nothing intrinsically wrong with including
files from within include files, I find it simpler to avoid it except in
such special cases.
Writing out all the include files required by a given header is excellent
documentation and makes your code more maintainable and portable. I also
recommend the practice of explicitly including and standard library
routines you are using even if you know darned well that they are already
included in one of your own headers. Again, it's good documentation and
won't break if you decide to implement your header differently.
I'm currently documenting the relationships between classes in my project.
I'm using the open source product Umbrello, which I find excellent for
drawing UML diagrams. To help with the documentation, I include a
'Relationships' section in each of my header files. This contains class
members that identify objects of other classes. For example, if an object
of class A maintains a pointer to an object of class B, then I place the
declaration of the pointer to B within the Relationships section of the
header for class A. This means that there is then a correspondence between
the relationships shown on my UML diagrams and the Relationships section of
my header file.
Another key kind of relationship is where a class uses another class without
maintaining a pointer. In such cases the dependency relationship is evident
from the code because the user class must include the header for the class
it is using; I can therefore use the #include statements in my code to
determine what dependency relationships I should show on my UML diagrams.
Another reason for not including from within include files is
that such dependencies are clearer. If I know that none of my headers
include other headers, then if a class A includes the header for class B, I
know that this means that A depends on B; I don't have to worry that it
might also be dependent on something else included by the header for B.