And*_*cia 7 c++ software-design include
场景:
foo.h中:
#include <vector>
class foo {
public:
std::vector<int>* getVector();
/* ... other methods declarations ... */
}
Run Code Online (Sandbox Code Playgroud)
Foo.cpp中:
#include "foo.h"
#include <vector>
/* ... other methods definitions using std::vector ... */
std::vector<int>* foo::getVector() {
return new std::vector<int>();
}
Run Code Online (Sandbox Code Playgroud)
我希望.cpp独立于标头中未来可能的任何变化.如果由于某种原因,类的接口发生了变化并且依赖性<vector>可以被消除,我冒着.cpp中的其他方法也会失去包含的风险.
重复包含<vector>在.cpp和.h中是否正确?这种做法是否有意义,还是仅仅依靠标题中包含的内容?
riw*_*alk 16
包括你需要的东西,仅此而已.
在多个.h文件和多个.cpp文件中包含相同的头文件本身不是问题.标题保护可有效减轻多次包含文件的问题.
如果你开始尝试避免包括相同的文件多次,它实际上是负面的,因为它通常会导致一个"巨型包含文件",其中包括所有你在整个项目所需要的.这很糟糕,因为对任何头文件的一次更改会导致所有内容重新编译.
如果您担心同时包含相同文件的.h/.cpp文件,请遵循以下准则:
| 归档时间: |
|
| 查看次数: |
1255 次 |
| 最近记录: |