Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Sometimes, a header file uses objects that are in declared in other header files. The user of this header file would have to include the dependency anyway and get the order correct too.

share|improve this question
A should be ... compilable. GCC will happily compile your .H (without any .C or .CPP) and point out any errors. There should be none. Ergo, if your header file relies on types, enums, etc, then it must #include the appropriate header file(s) to define them. – Mawg Mar 24 '15 at 13:04
up vote 1 down vote accepted

Assuming we are talking about C/C++ like languages that use #include

It is best (like all rules of thumb there are exceptions) to include only the things you explicitly need.

Anything that is not explicitly needed should be forward declared. This helps to prevent cycle dependencies in the code. Also when you do maintenance on the code it prevent you accidentally removing dependencies that have been implicitly introduced.

Full class definitions are needed if:

  • You inherit from them
  • You have member objects of that type (not pointers or references).
  • You have parameters passed by value of that type.

If you just use something by reference then you should try and use a forward declaration.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.