设想:
富.h:
#include <vector>
class foo {
public:
std::vector<int>* getVector();
/* ... other methods declarations ... */
}
foo.cpp:
#include "foo.h"
#include <vector>
/* ... other methods definitions using std::vector ... */
std::vector<int>* foo::getVector() {
return new std::vector<int>();
}
我希望 .cpp 独立于标题中任何可能的未来更改。如果由于某种原因类的接口发生变化并且<vector>
可以消除依赖关系,我冒着 .cpp 中的其他方法也失去该包含的风险。
<vector>
在 .cpp 和 .h 中重复包含是否正确?这种做法是否有意义,还是我应该只依赖标题中包含的内容?