我想知道在include文件中直接包含include语句和在源文件中包含include语句有什么优缺点。
个人而言,我喜欢让我的include文件“干净”,这样当我将它们包含到某个c / cpp文件中时,我不必寻找每个可能需要的标头,因为include文件本身会处理。另一方面,如果我在include文件中包含includes,编译时间可能会更长,因为即使使用了include保护,文件也必须首先被解析。这只是品味问题,还是其中一个方面有优缺点?
我的意思是:
sample.h
#ifdef ...
#include "my_needed_file.h"
#include ...
class myclass
{
}
#endif
sample.c
#include "sample.h"
my code goes here
Versus:
sample.h
#ifdef ...
class myclass
{
}
#endif
sample.c
#include "my_needed_file.h"
#include ...
#include "sample.h"
my code goes here
sample.h
和sample.c
都最终说#include“my_needed_file.h”
的情况下,可能是间接的吗?还是my_needed_file.h
包含另一个头文件已经包含的内容?例如,几个头文件都#include <vector>
?不太清楚您所询问的内容以及您认为会增加编译时间的原因。 - Jonathan Wakely