简体   繁体   English

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

[英]Should I include files included in another header?

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 ah in bh . 例如,假设类C继承自包含类A的类B.现在当然因为类B包含类A作为成员,所以它需要在bh包含ah However, let's say C also needs to include ah . 但是,让我们说C还需要包括ah Being lazy as I am, I just include bh (which C needs to include anyways), and since bh already includes ah , I don't need to include anything more, and it compiles fine. 像我一样懒惰,我只是包括bh (C需要反正包括),并且因为bh已经包含ah ,我不需要包含更多内容,并且它编译得很好。 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. 对于我的.cpp文件也是如此:我只包含标题,标题中包含的任何内容都将自动包含在我的.cpp文件中,所以我不在那里包含它。

Is this a bad habit of mine? 这是我的坏习惯吗? Does it make my code less readable? 它是否会降低我的代码的可读性?

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

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 ch declares a class C which inherits from class B , obviously you must include Bh . 在你的情况下,如果ch声明一个继承自class B class C class B ,显然你必须包含Bh However, if class A never appears in ch , I believe there is no reason to include it. 但是,如果class A从未出现在ch ,我相信没有理由将它包括在内。 The fact that bh mentions A means that bh must make what's necessary to be parsed, either through forward declaring A or including ah . bh提到A的事实意味着bh必须通过前向声明A或包括ah来做出必要的解析。

So from my point of view, you're doing what should be done. 所以从我的角度来看,你正在做着应该做的事情。

Also note that if for some reasons ch starts mentioning A , I would add the appropriate include or forward declaration so I wouldn't depend on the fact that bh does it for me. 还要注意,如果由于某些原因ch开始提到A ,我会添加适当的包含或转发声明,所以我不会依赖于bh为我做的事实。

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, eg a hash table implementation B that uses a hashing mechanism A B类使用A类,例如使用散列机制A的散列表实现B.

  • You create a class C that needs a hash table (ie B) and a hash algorithm (ie A) for some other purpose. 您创建一个需要哈希表(即B)和哈希算法(即A)的C类用于其他目的。 You include Bh and leave out Ah since Bh includes it anyway. 你包括Bh并且因为Bh包括它而遗漏了Ah。

  • 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. 玛丽,你的同事之一,发现了一篇关于这种新的神话般的哈希算法的论文,该算法可以减少碰撞的可能性,同时它需要的空间减少10%,速度是原来的两倍。 She (rightly) rewrites class B to use class D, which implements that algorithm. 她(正确地)重写了B类以使用D类,它实现了该算法。 Since class A is no longer needed in B, she also removes all references to it from Bh 由于B中不再需要A类,因此她也从Bh中删除了对它的所有引用

  • Your code breaks. 你的代码中断了。

EDIT 2: 编辑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. 有一些程序员(我有时也会因为匆忙而犯这个问题)通过在他们的项目中使用“include-all”头文件来处理这个问题。 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. 是的,在我看来,MSVC中的windows.h就是其中一个案例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该包含已经通过其他标头包含的标头吗? - should I include a header that is already included via other headers? 我应该在源文件中包含包含命名空间定义的 header 吗? - Should I include the header containing the definition of a namespace in source files? 我应该将 #include.cpp 文件添加到 TestSuite 的 header 以使 cxxtest 工作吗? - Should I #include .cpp files to header of TestSuite to make cxxtest work? 为什么我不应该包含 cpp 文件而是使用标题? - Why should I not include cpp files and instead use a header? 如果stdafx.h中已包含特定的标头-我是否需要(必须/应该)将其显式包含在.cpp文件中? - If a particular header already included in stdafx.h - do I need to (have to/should to) to explicitly include it in a .cpp file? 我应该包括每个标题吗? - Should I include every header? 如果我需要在标头中包含另一个标头,并且打算在CPP文件中使用它,那么是否也应该在其中包含它以提高可读性? - If I need to include another header in my header and I plan to use it in my CPP file, should I include it there as well for readability? 确定所有所需的头文件是否包含在另一个头文件中 - Determine if all needed header files are included in another header file Header 应包含一次 - Header should be included once CMake:无法在我的库的 header 文件中包含外部库 header 文件,这些文件未包含在任何源文件中 - CMake: Unable to include external library header files in my library's header files that are not included in any source files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM