C++ 头文件顺序

2022-01-11 00:00:00 header include c++ code-organization

header/cpp 文件中的 headers 应该以什么顺序声明?显然,后续标头所需的那些应该更早,并且特定于类的标头应该在 cpp 范围内而不是标头范围内,但是是否有固定的顺序约定/最佳实践?

What order should headers be declared in a header / cpp file? Obviously those that are required by subsequent headers should be earlier and class specific headers should be in cpp scope not header scope, but is there a set order convention / best practice?

推荐答案

在头文件中,您必须包含所有头文件才能使其可编译.并且不要忘记使用前向声明而不是某些标头.

In a header file you have to include ALL the headers to make it compilable. And don't forget to use forward declarations instead of some headers.

在源文件中:

  • 对应的头文件
  • 必要的项目标题
  • 第三方库标头
  • 标准库头文件
  • 系统标头

按此顺序,您将不会错过任何忘记自己包含库的头文件.

In that order you will not miss any of your header files that forgot to include libraries by their own.

相关文章