Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

1 Answer 1

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

 
discard

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.