.cpp文件中可以包含(不包括)什么?

时间:2020-06-01 20:15:15

标签: c++ include header-files

这个问题很奇怪。 假设我有一个名为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个标头了? (假设) 抱歉,这是一个很愚蠢的问题,但我不要经常使用很多标头。

1 个答案:

答案 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.hidiot.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>本身。

相关问题