0

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.

  • A en.wikipedia.org/wiki/Single_Compilation_Unit 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
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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