把包括在所有正确的地方

时间:2021-03-22 15:07:42

So, we use #include to tell the compiler/linker where to find all the relevant code for a class.

因此,我们使用#include告诉编译器/链接器在哪里找到类的所有相关代码。

Have a look at this:

看看这个:

ClassC.h:
    #include "ClassA.h";

ClassD.h:
    #include "ClassC.h"

Let's say we use ClassA in our ClassD, but in a way that is in no way connected to ClassC. We have implicitly included it by including ClassC, so no error is thrown. However, should the code change and ClassC is no longer needed (and we remove the #include "ClassC.h" because of that), we'd get undefined reference error since ClassA is now unreferenced.

假设我们在ClassD中使用ClassA,但这种方式与ClassC无关。我们通过包含ClassC隐式包含它,因此不会抛出任何错误。但是,如果代码更改并且不再需要ClassC(并且我们因此删除#include“ClassC.h”),我们将得到未定义的引用错误,因为ClassA现在未被引用。

Is there a way to have the compiler/linker look at each of the *.cpp and assigned *.h independently, without looking into included .h files?

有没有办法让编译器/链接器独立查看每个* .cpp并分配* .h,而不查看包含的.h文件?

If that were the case, it wouldn't look into ClassC.h and see it includes ClassA.h, but throw an error or at least a warning telling me that I didn't include ClassA.h. It makes more sense since, as far as class ClassD knows, ClassA and ClassC have no connection to one another (under the set assumption I'm using ClassA independently of ClassC).

如果是这种情况,它将不会查看ClassC.h并看到它包含ClassA.h,但抛出一个错误或至少一个警告告诉我我没有包含ClassA.h。它更有意义,因为就类ClassD所知,ClassA和ClassC彼此之间没有连接(在设定的假设下我使用ClassA独立于ClassC)。

I think that the code written that way would be much more stable and resilient to change. Wouldn't it?

我认为以这种方式编写的代码会更稳定,更有弹性。不是吗?

Also, let's say we build a project, and it works. If we were to then reference some class(X) earlier in code than it was referenced before, we might get an error if it was using some other class(Y) that was included from some other source between that point in the program, and the previous first occurrence of that class. If we tied includes more closely to each header file that explicitly uses it, such errors would never appear.

另外,假设我们建立了一个项目,它的确有效。如果我们之前在代码中引用某些类(X)而不是之前引用的类,那么如果它正在使用某些其他类(Y)从程序中该点之间的某些其他源中包含,那么我们可能会收到错误。该类的前一次出现。如果我们捆绑包含更接近于明确使用它的每个头文件,那么这些错误将永远不会出现。

I know I could keep track of all this myself, but it would be so easy to miss a few or more in larger projects, even medium ones. Besides, having an error/warning would make it so much easier to manage.

我知道我可以自己跟踪所有这些,但是在较大的项目中,甚至是中等项目中,很容易错过一些或更多项目。此外,出现错误/警告会使管理变得更加容易。

I know I'm complicating things a bit, but this seems a good practice to me.

我知道我的事情有点复杂,但对我来说这似乎是一个很好的做法。

It this even a good idea and can it somehow be set in compiler/linker options (e.g. GNU gcc compiler in CodeBlocks)?

这甚至是一个好主意,它可以以某种方式设置在编译器/链接器选项中(例如CodeBlocks中的GNU gcc编译器)?

1 个解决方案

#1


1  

Let's say we use ClassA in our ClassD, but in a way that is in no way connected to ClassC. We have implicitly included it by including ClassC, so no error is thrown. However, should the code change and ClassC is no longer needed (and we remove the #include "ClassC.h" because of that), we'd get undefined reference error since ClassA is now unreferenced.

假设我们在ClassD中使用ClassA,但这种方式与ClassC无关。我们通过包含ClassC隐式包含它,因此不会抛出任何错误。但是,如果代码更改并且不再需要ClassC(并且我们因此删除#include“ClassC.h”),我们将得到未定义的引用错误,因为ClassA现在未被引用。

If you need to use ClassA in your ClassD, then explicitly #include "ClassA.h" in your ClassD.h. Do not rely on implicit chains of inclusions.

如果您需要在ClassD中使用ClassA,那么在ClassD.h中显式地#include“ClassA.h”。不要依赖隐含的包含链。

#1


1  

Let's say we use ClassA in our ClassD, but in a way that is in no way connected to ClassC. We have implicitly included it by including ClassC, so no error is thrown. However, should the code change and ClassC is no longer needed (and we remove the #include "ClassC.h" because of that), we'd get undefined reference error since ClassA is now unreferenced.

假设我们在ClassD中使用ClassA,但这种方式与ClassC无关。我们通过包含ClassC隐式包含它,因此不会抛出任何错误。但是,如果代码更改并且不再需要ClassC(并且我们因此删除#include“ClassC.h”),我们将得到未定义的引用错误,因为ClassA现在未被引用。

If you need to use ClassA in your ClassD, then explicitly #include "ClassA.h" in your ClassD.h. Do not rely on implicit chains of inclusions.

如果您需要在ClassD中使用ClassA,那么在ClassD.h中显式地#include“ClassA.h”。不要依赖隐含的包含链。