#include如何在C ++中工作? [英] How does #include work in C++?

查看:103
本文介绍了#include如何在C ++中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果类头中包含一个库,然后另一个类中包含了该头,我是否必须再次包含该库?

If a library is included in a class header and then this header is included in another class do I have to include the library again?

例如:

#ifndef A_H
#define A_H

#include<someLibrary.h>

class A{
  ...
}

#endif

然后另一个类包含Ah头

Then another class includes the A.h header

#include<A.h>   //include class A
class B{
   ...
}

我必须在类B中包含 someLibrary.h吗?

Do I have to include the "someLibrary.h" in class B?

推荐答案

不, #include 是可传递的。

但是,如果您的第二个文件本身使用了 someLibrary 中的符号,那就很好了样式以重新包括标题。这样,您就不会希望并祈祷您永远不会删除中间包含。如果每个源文件 #include 都能满足其直接需求,则您的代码库将更加强大。标头保护符可以防止这成为浪费的政策。

However, if your second file itself uses symbols from someLibrary, it's good style to re-include the header. That way you're not "hoping and praying" that you never remove the intermediate include. Your codebase will be more robust if every source file #includes everything that it directly needs. Header guards prevent this from being a wasteful policy.

这篇关于#include如何在C ++中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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