Assuming that all headers are guarded, let’s say you had an abstract data type.
#include "that.h"
#include "there.h"
class Foo {
protected:
// Functions that do stuff with varOne and varTwo
private:
that varOne;
there varTwo;
...
};
Then in the classes that inherit from foo ( and thus include foo.h ), would you also bother including that and this? Normally what I do is include everything that a class needs, regardless of whether would already receive them from another include. Is this redundant?
There is one downside to redundantly including header files that would otherwise have been included directly: it forces the compiler to reopen and reparse them. For example, in
a.h:
b.h:
c.cpp:
a.h has to be opened and read twice — although the
#ifndefwill make the preproc ignore a.h’s contents in the second inclusion, it still has to at least load the file from disk and read its bytes from the#ifndefto the#endif.This can slow down compilation. It will not break your build or anything though, it’ll just be an annoyance in really large projects, where compiles can take many minutes.
If you use
#pragma oncein your headers, there’s a good chance an aware compiler will cache the filenames at a higher level, and ignore the second inclusion of a.h altogether. That too can speed up builds.