including multiple headers
-
Is it bad practice to have a "MyProjectHeaders.h" file that has the #ifndef......#endif protection around a set of include statements ? Then just include the "MyProjectHeaders.h" file in each source ? I know compilation/linking will take longer and there will be redundant headers. I played around with it and found that I had to include some specific headers in each source. The API includes do not seem to suffer from this (you can include an API header that has it's own include statements and access their functionality without including the specific file in the source). Can anyone shed some light on this ? Is it a VC++ 6.0 linking/settings issue ? Cheers ;-) If sex is a pain in the ass, then you are doing it all wrong!
-
Is it bad practice to have a "MyProjectHeaders.h" file that has the #ifndef......#endif protection around a set of include statements ? Then just include the "MyProjectHeaders.h" file in each source ? I know compilation/linking will take longer and there will be redundant headers. I played around with it and found that I had to include some specific headers in each source. The API includes do not seem to suffer from this (you can include an API header that has it's own include statements and access their functionality without including the specific file in the source). Can anyone shed some light on this ? Is it a VC++ 6.0 linking/settings issue ? Cheers ;-) If sex is a pain in the ass, then you are doing it all wrong!
It's not bad practice, it's a good practice; better would be to remove the header dependency by using forward declaration, or pointer to objects in your classes instead of "direct" class members
/// would be ok.
...
#ifndef _A_CLASS_H_
#include "AClass.h"
#endifclass CAnotherClass
{
CAClass m_AClass;
}/// would be better.
...
class CAClass;
class CAnotherClass
{
CAClass* m_pAClass;
}In the long run, including headers in cpp files will not be as costly as including them in header files ( see "Large Scale C++ Software Design" by Lakos ) if you need to include a file in another header file, you'd better use #ifndef ... #end around the include directive to limit the damages.
Maximilien Lincourt "Never underestimate the bandwidth of a station wagon filled with backup tapes." ("Computer Networks" by Andrew S Tannenbaum )