标题背后的理由是什么?
我不太明白有标题的意义;这似乎违反了 DRY 原则!标头中的所有信息都(可以)包含在实现中.
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.
推荐答案
它简化了编译过程.当你想独立编译单元时,你需要一些东西来描述将链接到的部分,而不必导入所有其他文件的全部.
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.
它们不是解决这些问题的唯一方法,但 30 年前它们是一个很好的方法.我们今天可能不会将头文件用于一种语言,但它们并不是 2009 年发明的.
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.
相关文章