头文件的优点/缺点

发布于 2024-10-27 06:16:17 字数 150 浏览 1 评论 0原文

与 Java 等语言相比,在 C 或 C++ 等语言中使用头文件有哪些优点和缺点?我认为类应该从外向内设计,因此最好有头文件,而不必费力地了解实现细节。然而,话又说回来,每个函数声明都会在两个文件中重复。如果今天发明了 C 和 C++,它们会使用头文件吗?这种机制是否已经过时或有必要?

What are the advantages and disadvantages of using header files in a language like C or C++ verses a language like Java? I think classes should be designed from the outside in, so it is nice to have header files and not have to wade through implementation details. However, then again, each function declaration is duplicated across two files. If C and C++ were invented today would they use header files? Is this mechanism outdated or necessary?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遗失的美好 2024-11-03 06:16:17

摘自 Eric Lippert 的相关博客文章,谁说得很好:

我会问同等的
问题为什么C++需要头文件
文件
?头文件看起来很大
潜在的故障点;所有的
当我编辑 C++ 代码并更改
方法的签名;如果我忘记了
更新头文件,然后更新代码
不编译并且经常给出一些
神秘的错误消息。希望这个
巨大的成本实际上买下了你
东西。

它为编译器编写者带来了一件事,
和用户是一回事。

它给用户带来的是你可以
编译每个单独的“cpp”文件
独立成一个“obj”文件,
前提是你拥有所有
必要的标头。所有信息
生成物体所必需的
可以收集给定的 cpp 文件中的内容
来自标题集。这意味着
构建系统可以重新编译
只是那些改变的cpp文件,
前提是标题没有改变。

它为编译器编写者带来的是
每个文件都可以编译成
“一次通过”。因为每种类型和
方法声明之前处理过
第一次使用时,编译器可以
只需从文件顶部开始,
拉入所有包含的标头,并且
从上到下进行,吐痰
直接输出 obj 文件,永远不会
必须回去重访
已经看到的东西。

这与 C#(博客文章讨论的内容)和 Java 等语言形成鲜明对比,后者是近亲。

Taken from a related blog post by Eric Lippert, who puts it very well:

I would have asked the equivalent
question why does C++ need header
files
? Header files seem like a huge
potential point of failure; all the
time I edit C++ code and change the
signature of a method; if I forget to
update the header file, then the code
doesn’t compile and often gives some
cryptic error message. Hopefully this
large cost actually buys you
something.

It buys the compiler writer one thing,
and the user one thing.

What it buys the user is that you can
compile each individual “cpp” file
into a “obj” file independently,
provided that you have all the
necessary headers. All the information
necessary to generate the bodies that
are in a given cpp file can be gleaned
from the set of headers. This means
that the build system can recompile
just those cpp files that changed,
provided that no header changed.

What it buys the compiler writer is
that every file can be compiled in
“one pass”. Because every type and
method declaration is processed before
its first usage, the compiler can
simply start from the top of the file,
pull in all the included headers, and
proceed from the top down, spitting
out the obj file as it goes, never
having to go back and revisit
something its seen already.

This is in contrast to languages such as C# (about which the blog post is) and Java, which is a pretty close relative.

遥远的她 2024-11-03 06:16:17

将接口和实现分开仍然是一个好主意。但这不一定是物理分离。在Java中你可以从javadoc中看到接口。 Java IDE 通常可以显示 API 结构,并且可以折叠块。没有令人信服的理由需要物理隔离。 C 是几十年前发明的,所以我们不需要挑剔它。

It's still a good idea to separate interface and implementation. But it doesn't have to be physical separation. In Java you can see the interface from javadoc. Java IDEs usually can display API structures, and they can fold blocks. There is no compelling reasons that require physical separation. C was invented decades ago so we don't need to pick on it.

猫腻 2024-11-03 06:16:17

简而言之,在 C 或 C++ 中,头文件允许不同的文件共享通用的定义或声明。

在Java中,一切都是对象,因此除了对象之外不存在共享任何东西的概念。每个对象都是一个文件;如果您想访问该对象,请导入该文件。

Briefly, in C or C++ header files allow different files to share common definitions or declarations.

In Java everything is an object, so there's no concept of sharing anything except objects. Each object is one file; if you want to access the object, you import the file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文