我应该包含另一个标头中包含的文件吗?

发布于 2024-10-07 06:56:52 字数 387 浏览 0 评论 0原文

大多数情况下,当在程序中创建相互使用的多个类时,我喜欢只包含所需的最少数量的头文件,以减少混乱。

例如,假设类 C 继承自类 B,类 B 包含类 A。当然,由于类 B 包含类 A 作为成员,因此它需要在 bh 中包含 ah 。然而,假设 C 也需要包含 ah。由于我很懒,我只包含 bh (无论如何 C 都需要包含它),并且由于 bh 已经包含 ah,所以我不'不需要包含更多内容,并且编译得很好。我的 .cpp 文件也是如此:我只包含标头,标头中包含的任何内容都会自动包含在我的 .cpp 文件中,因此我不将其包含在那里。

这是我的坏习惯吗?它会降低我的代码的可读性吗?

Most often when creating multiple classes inside a program that use each other, I like to include only the minimum number of header files I need to reduce clutter.

For example, say class C inherits from class B, which contains class A. Now of course since class B contains class A as a member, it needs to include a.h in b.h. However, let's say C also needs to include a.h. Being lazy as I am, I just include b.h (which C needs to include anyways), and since b.h already includes a.h, I don't need to include anything more, and it compiles fine. Same for my .cpp files: I just include the header, and anything that's included in the header will be automatically included in my .cpp file, so I don't include it there.

Is this a bad habit of mine? Does it make my code less readable?

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

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

发布评论

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

评论(3

抠脚大汉 2024-10-14 06:56:52

我坚持这个简单的规则:包含完全声明给定类所需的所有内容,但不要包含更多内容,并且不假设包含从其他来源拉入,即确保您的文件是自给自足的。

I stick with this simple rule: include everything you need to completely declare a given class, but not more and make no assumptions about includes being pulled in from other sources, i.e. ensure your files are self-sufficient.

A君 2024-10-14 06:56:52

包含解析头文件所需的内容,而不依赖于外部包含顺序(换句话说:使您的标头自给自足)。

在你的例子中,如果ch声明了一个继承自class Bclass C,显然你必须包含Bh。但是,如果 class A 从未出现在 ch 中,我相信没有理由包含它。 bh 提到 A 的事实意味着 bh 必须通过前向声明 A 来进行解析所需的内容或者包括ah

所以从我的角度来看,你正在做应该做的事情。

另请注意,如果由于某些原因 ch 开始提及 A,我会添加适当的包含或前向声明,这样我就不会依赖于 bh< /code> 为我做的。

Include what's necessary for the header file to be parsed without relying on external include ordering (in other words : make your headers self-sufficient).

In your case, if c.h declares a class C which inherits from class B, obviously you must include B.h. However, if class A never appears in c.h, I believe there is no reason to include it. The fact that b.h mentions A means that b.h must make what's necessary to be parsed, either through forward declaring A or including a.h.

So from my point of view, you're doing what should be done.

Also note that if for some reasons c.h starts mentioning A, I would add the appropriate include or forward declaration so I wouldn't depend on the fact that b.h does it for me.

格子衫的從容 2024-10-14 06:56:52

最好将每个标头都包含在您直接使用的定义中。

依赖其他标头之一来包含内容会使您的代码更加脆弱,因为它变得依赖于其外部类的实现。

编辑:

一个简短的示例:

  • B 类使用 A 类,例如使用哈希机制 A 的哈希表实现 B

  • < p>您创建了一个类 C,它需要一个哈希表(即 B)和一个哈希算法(即 A)用于其他目的。您包含 Bh 并省略 Ah,因为 Bh 无论如何都包含它。

  • 玛丽,你的一位同事,发现了一篇关于这种新的令人难以置信的哈​​希算法的论文,该算法降低了冲突的可能性,同时它需要的空间减少了 10%,速度提高了一倍。她(正确地)重写了 B 类以使用 D 类,D 类实现了该算法。由于 B 中不再需要 A 类,因此她还从 Bh 中删除了对它的所有引用

  • 您的代码中断。

编辑2:

有一些程序员(我偶尔也会在匆忙时对此感到内疚)通过在他们的项目中使用“include-all”头文件来处理这个问题。应该避免这种情况,因为它会导致前所未有的命名空间污染。是的,我认为 MSVC 中的 windows.h 就是其中之一。

It's best to include every header with definitions that you are using directly.

Relying on one of the other headers to include stuff makes your code more fragile as it becomes dependent on the implementation of classes that are external to it.

EDIT:

A short example:

  • Class B uses class A, e.g. a hash table implementation B that uses a hashing mechanism A

  • You create a class C that needs a hash table (i.e. B) and a hash algorithm (i.e. A) for some other purpose. You include B.h and leave out A.h since B.h includes it anyway.

  • Mary, one of your co-workers, discovers a paper about this new fabulous hashing algorithm that reduces the probability of collisions, while it needs 10% less space and is twice as fast. She (rightly) rewrites class B to use class D, which implements that algorithm. Since class A is no longer needed in B, she also removes all references to it from B.h.

  • Your code breaks.

EDIT 2:

There are some programmers (and I've occasionally been guilty of this too, when in a hurry) who deal with this issue by having an "include-all" header file in their project. This should be avoided, since it causes namespace pollution of unparalleled proportions. And yes, windows.h in MSVC is one of those cases in my opinion.

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