标题背后的理由是什么? [英] What's the rationale behind headers?

查看:21
本文介绍了标题背后的理由是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太明白有标题的意义;这似乎违反了 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.

这篇关于标题背后的理由是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆