从包含文件包含的最佳实践 [英] Best practice for including from include files

查看:55
本文介绍了从包含文件包含的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否直接在包含文件中包含include语句,而不是在源文件中包含include语句.

I was wondering if there is some pro and contra having include statements directly in the include files as opposed to have them in the source file.

我个人比较喜欢包含"clean",因此,当我将它们包含在某些c/cpp文件中时,由于包含文件本身并不能解决问题,因此我不必寻找所有可能需要的标头.另一方面,如果我在包含文件中包含了包含项,则编译时间可能会变长,因为即使有了包含保护,也必须首先解析这些文件.这只是口味问题,还是其他优点/缺点?

Personally I like to have my includes "clean" so, when I include them in some c/cpp file I don't have to hunt down every possible header required because the include file doesn't take care of it itself. On the other hand, if I have the includes in the include files compile time might get bigger, because even with the include guards, the files have to be parsed first. Is this just a matter of taste, or are there any pros/cons over the other?

我的意思是:

sample.h

#ifdef ...

#include "my_needed_file.h"
#include ...

class myclass
{
}

#endif

sample.c

#include "sample.h"

my code goes here

对:

sample.h

#ifdef ...

class myclass
{
}

#endif

sample.c

#include "my_needed_file.h"
#include ...
#include "sample.h"

my code goes here

推荐答案

实际上并没有任何标准的最佳实践,但是对于大多数帐户,您应该在标题中包含您真正需要的内容,并且向前声明您可以做什么.

There's not really any standard best-practice, but for most accounts, you should include what you really need in the header, and forward-declare what you can.

如果实现文件需要标头没有明确要求的内容,则该实现文件应包括其自身.

If an implementation file needs something not required by the header explicitly, then that implementation file should include it itself.

这篇关于从包含文件包含的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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