A few questions about headers

Hello!
These questions have probably been answered a thousand times, but I really can't find a clear answer on the internet.

Is the general rule of headers just to not have recursion? (As in header A.h includes B.h, which includes C.h, which includes A.h).

Also, including a header A.h into two other headers B.h and C.h, and then including B.h and C.h into D.h - is this considered a bad practice?

Thank you!
Is the general rule of headers just to not have recursion?
Yes, actually it is not possible. I.e. compiler error. I many cases it is a hint for bad design.

Also, including a header A.h into two other headers B.h and C.h, and then including B.h and C.h into D.h - is this considered a bad practice?
No, the best pratice is to include headers only when you need them.
The most important thing about headers to is avoid defining functions or variables inside the header. Otherwise you're setting yourself up for "multiply defined symbol" errors if two source files include the header. You should declare code/variables in headers and define them in source files. There are exceptions. It's okay to define inline functions/methods or const variables.

Another cardinal sin is putting a "using" statement in a header (e.g., using namespace std;). That "using" functionality carries on to any other headers that might be included later and can have unexpected consequences. If your header needs something from a namespace (like std::string) then use the fully qualified name (e.g. std::string).
Hello daverave1212,

Expanding on what dhayden has said you may find this worth reading:

http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

Andy
Topic archived. No new replies allowed.