这个问题很奇怪。 假设我有一个名为idiot.cpp的文件,它的开头是:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <string>
#include <set>
#include <exception>
#include <iostream>
#include "idiot.h"
,我有一个头文件idiot.h。首先,在idiot.h中,我也必须插入
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <string>
#include <set>
#include <exception>
#include <iostream>
还是我不?
其次,如果我还有另一个使用idiot.h的源文件“ bonito.cpp”,我应该只是#include "idiot.h"
,还是应该再次粘贴代码片段?
如果没有,有什么办法可以缩短它,这样我就不会在每个文件中包含大约20个标头了? (假设)
抱歉,这是一个很愚蠢的问题,但我不要经常使用很多标头。
答案 0 :(得分:2)
idiot.cpp
不需要包含idiot.h
间接包含的任何内容,即可进行编译。
更有趣的情况:bonito.cpp
#include
的{{1}}和idiot.h
包括idiot.h
。如果x.h
仅 使用idiot.h
的内容作为私有或匿名命名空间代码-并且x.h
也想使用bonito.cpp
中的内容,则{ {1}}应该直接(冗余地)包含x.h
。
理由很简单:bonito.cpp
不能在不破坏客户端代码的风险的情况下从公共/受保护的接口中删除某些内容,因此,如果发生这种情况,客户端必须期望处理它-可能包括一个额外的标头太。但是,x.h
和idiot.h
的维护者应该可以自由更改idiot.h
中的私有或匿名命名空间内容,而不必担心破坏客户端代码,包括包含哪些标头来支持该私有代码。 / p>
因此,包含在完成时是多余的,但是这样做是希望随着idiot.cpp
的发展它可能不再是多余的。
此最佳做法模型不会自动执行-您必须了解它的工作方式并主动对该模型进行编码。
如果idiot.h
包含...
idiot.h
...然后idiot.h
可以合理地使用#include <string>
#include <vector>
...
class X {
void add(const std::string&);
private:
std::vector<std::string> v_;
};
而不包含bonito.cpp
,但是如果需要std::string
,则应该包含<string>
本身。