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语句放在A.h或A.cpp中吗?
5 个解决方案
#1
22
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.
这是一个有争议的领域。我自己的偏好是每个标题应该能够独立,所以如果它需要其他标题,它包含它们。换句话说,如果客户端代码无论如何都需要包含
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.
如果您确实需要包含
#2
3
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.
将其包含在需要的地方。如果你在类的声明中使用
#3
2
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中。这样它就不会包含在其他cpp文件中,这些文件可能包含你的A.h但不需要iostream。除非由于某种原因,您的头文件中有一些东西需要iostream。但如果是这样的话,你可能会做错其他的事......
#4
1
It depends.
If you use the 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
如果您只使用类声明,则可以使用不完整的类型。在这种情况下,在头文件中包含
#5
0
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文件中。
#1
22
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.
这是一个有争议的领域。我自己的偏好是每个标题应该能够独立,所以如果它需要其他标题,它包含它们。换句话说,如果客户端代码无论如何都需要包含
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.
如果您确实需要包含
#2
3
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.
将其包含在需要的地方。如果你在类的声明中使用
#3
2
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中。这样它就不会包含在其他cpp文件中,这些文件可能包含你的A.h但不需要iostream。除非由于某种原因,您的头文件中有一些东西需要iostream。但如果是这样的话,你可能会做错其他的事......
#4
1
It depends.
If you use the 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
如果您只使用类声明,则可以使用不完整的类型。在这种情况下,在头文件中包含
#5
0
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文件中。