方式包括许多头文件 [英] ways to include many header files

查看:93
本文介绍了方式包括许多头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我遇到了code处理这头文件这种方式。
会有所谓的说,一个头文件 global.h
global.h 将包括一些其他的头文件,如,

Recently I came across code which handled header files this way. There would be one header file called say global.h This global.h would include some other header files, e.g.,

#include "settings.h"
#include "math.h"
#include "somelibrary.h"
#include "someOtherlibrary.h"
...

现在,每当某些文件希望包括比方说 somelibrary.h ,而不是写作
的#include somelibrary.h 它只是包含 global.h
因此,在项目中的每个源文件只是有:的#includeglobal.h

Now, whenever some file wanted to include say somelibrary.h, instead of writing #include somelibrary.h it would just include global.h. So each source file in the project just had: #include "global.h"

是为了避免写很多,包括在每一个源文件这种常用的方法?有什么其他的好处

Is this common way to avoid writing many includes in each source file? what are other benefits

PS。额外的:这将是很好,如果有人可以解释为什么这工作

ps. extra: it would be nice if someone could explain why this works

推荐答案

这是一个常用的的做法。如果使用global.h,您可以创建文件和项目,这反过来又创造项目的每一个模块之间的紧耦合的各模块之间的紧密耦合。

This is a commonly used bad practice. If you use a global.h, you create a tight coupling between that file and every module in the project, which in turn creates a tight coupling between every single module of your project.

这是非常糟糕的OO设计!希望每个模块是自主的,只取决于是其自身功能实际上至关重要等模块。更进一步,你会一直想要设计一个模块,使他们能够在多个项目中使用。

That is very bad OO design! You want each module to be autonomous and only depend on other modules that are actually crucial for its own functionality. Further more, you will always want to design modules so that they can be used in several projects.

例如,如果我想用你的通用数学库模块,它已经为特定项目开发的,那么为什么在地球上,你会强迫我,包括从该项目以及所有其他不相关的文件?

For example, if I want to use your generic math library module, which you have already developed for a particular project, then why on earth would you force me to include every other unrelated file from that project as well?

至于避免写很多,包括在每一个源文件......这是完全超出了我怎么有在文件的顶部写文字的5-10极短的线路可以是一个程序员的一大障碍。显然,有很多谁觉得这是这样一个障碍的人,他们决定,因为的改变整个方案设计的它。如果你不喜欢敲击键盘,那么也许不用去为一个程序员的职业生涯。

As for "avoid writing many includes in each source file"... It is completely beyond me how having to write 5-10 very short lines of text at the top of a file can be a major obstacle to a programmer. Apparently there are plenty of people who find that to be such an obstacle, that they decide to change the whole program design because of it. If you don't like typing on the keyboard, then maybe don't go for a programmer career.

这篇关于方式包括许多头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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