C++ 在头文件或 cpp 中包含 std 库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7733197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:24:56  来源:igfitidea点击:

Include std library in header or cpp?

c++header-files

提问by user695652

If I have a class A which uses iostream, should I put the include statement of iostream in A.h or A.cpp?

如果我有一个使用iostream的A类,我应该将iostream的include语句放在Ah或A.cpp中吗?

回答by Jerry Coffin

This is an area of some controversy. My own preference is that each header should be able to stand alone, so if it needs other headers, it includes them. In other words, if client code is going to need to include <iostream>(or whatever) anyway, your header should handle that for them. OTOH, if the user of the iostream is strictly hidden so the client code doesn't need to include it at all, then it should only be included in the implementation file.

这是一个存在争议的领域。我自己的偏好是每个标题都应该能够独立存在,因此如果它需要其他标题,则包括它们。换句话说,如果客户端代码<iostream>无论如何都需要包含(或其他),您的标头应该为它们处理。OTOH,如果iostream的用户被严格隐藏,因此客户端代码根本不需要包含它,那么它应该只包含在实现文件中。

In many cases (especially where the header is open to frequent change), you'd prefer to avoid including it in the header. In such cases, the PImpl idiom can be useful to get the dependency out of the header.

在许多情况下(尤其是在标题可以频繁更改的情况下),您希望避免将其包含在标题中。在这种情况下,PImpl 习惯用法可用于从头中获取依赖项。

If you do need to include <iostream>, do your clients a favor and consider whether you can #include <iosfwd>instead of <iostream>though. This can improve compile time a fair amount.

如果您确实需要包含<iostream>,请帮您的客户一个忙,并考虑您是否可以#include <iosfwd>代替<iostream>尽管。这可以大大缩短编译时间。

回答by littleadv

Include it where its needed. If you use something defined in <iostream>in the declaration of the class (like a member variable, a member function parameter or return type, etc), then it should be in the H file. If you only use it in the implementation - then in the CPP.

将其包含在需要的地方。如果<iostream>在类的声明中使用了定义的东西(如成员变量、成员函数参数或返回类型等),那么它应该在 H 文件中。如果你只在实现中使用它 - 那么在 CPP 中。

回答by Kurtis Nusbaum

Include it in the cpp. That way it's not potentially included in other cpp files that may include your A.h but don't need the iostream. Unless of course for some reason there is something in your header file that needs iostream. But if that's the case you might be doing something else wrong...

将其包含在 cpp 中。这样它就不会包含在其他可能包含您的 Ah 但不需要 iostream 的 cpp 文件中。当然,除非出于某种原因,您的头文件中存在需要 iostream 的内容。但如果是这样的话,你可能做错了其他事情......

回答by sehe

It depends.

这取决于。

If you usethe classes in the header file, you need it in the header file (obviously).

如果使用头文件中的类,则在头文件中需要它(显然)。

If you just use the class declarations you can use incomplete types. In that case, include <iosfwd>in your header file, and <iostream>in the cpp

如果您只使用类声明,则可以使用不完整的类型。在这种情况下,包括<iosfwd>在您的头文件中,并<iostream>在 cpp 中

回答by Joe

Use it where it is needed.

在需要的地方使用它。

If your class declaration references types in the header, you will need to include it there. If it's only in the implementation, then you can include it in the cpp file.

如果您的类声明在标题中引用了类型,则需要将其包含在那里。如果它只是在实现中,那么您可以将它包含在 cpp 文件中。