Search code examples
c++cheaderprinciples

What's the rationale behind headers?


I don't quite understand the point of having a header; it seems to violate the DRY principle! All the information in a header is (can be) contained in the implementation.


Solution

  • It simplifies the compilation process. When you want to compile units independently, you need something to describe the parts that will be linked to without having to import the entirety of all the other files.

    It also allows for code hiding. One can distribute a header to allow others to use the functionality without having to distribute the implementation.

    Finally, it can encourage the separation of interface from implementation.

    They are not the only way to solve these problems, but 30 years ago they were a good one. We probably wouldn't use header files for a language today, but they weren't invented in 2009.