What#39;s the rationale behind headers?(标题背后的理由是什么?)
问题描述
我不太明白有标题的意义;这似乎违反了 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.
这篇关于标题背后的理由是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:标题背后的理由是什么?


基础教程推荐
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 我有静态或动态 boost 库吗? 2021-01-01