*.h 或 *.hpp 用于您的类定义

2022-01-11 00:00:00 header c++

我一直使用 *.h 文件作为我的类定义,但在阅读了一些 boost 库代码后,我意识到它们都使用 *.hpp.我一直讨厌那个文件扩展名,我想主要是因为我不习惯它.

I've always used a *.h file for my class definitions, but after reading some boost library code, I realised they all use *.hpp. I've always had an aversion to that file extension, I think mainly because I'm not used to it.

使用*.hpp相对*.h有什么优缺点?

What are the advantages and disadvantages of using *.hpp over *.h?

推荐答案

以下是 C 和 C++ 头文件命名不同的几个原因:

Here are a couple of reasons for having different naming of C vs C++ headers:

  • 自动代码格式化,对于格式化 C 和 C++ 代码,您可能有不同的准则.如果标题由扩展名分隔,您可以将编辑器设置为自动应用适当的格式
  • 命名,我参与过一些项目,其中有用 C 编写的库,然后用 C++ 实现了包装器.由于标头通常具有相似的名称,即 Feature.h 与 Feature.hpp,因此它们很容易区分.
  • 包含,也许您的项目有更合适的可用 C++ 编写的版本,但您使用的是 C 版本(见上文).如果标头以实现它们的语言命名,您可以轻松发现所有 C 标头并检查 C++ 版本.

请记住,C 不是 C++,除非您知道自己在做什么,否则混合搭配可能非常危险.适当地命名您的来源可以帮助您区分语言.

Remember, C is not C++ and it can be very dangerous to mix and match unless you know what you are doing. Naming your sources appropriately helps you tell the languages apart.

相关文章