我看到过这样的头文件风格,即头文件不包含其他头文件,相应的*.cpp文件必须包含所有依赖项(并且以正确的顺序包括它们)。在早期可能会使构建依赖跟踪更容易(但我只是猜测)。现在还有什么好的理由吗?
文件“B.h”:
文件“B.h”:
#ifndef _B_h_
#define _B_h_
// Note we do not #include "A.h" that contains class A declaration.
class B
{
public:
A a; // An A object.
};
#endif // _B_h_
"B.cpp" 文件:
#include "A.h" // Must include this before B.h, otherwise class A not defined in B.h
#include "B.h"
...
#include "foo.h"
开头。 - Kerrek SBb.cpp
以#include "b.h"
开始,那么你就无法使用这种结构了。 - Kerrek SB