*.h 或 *.hpp 用于您的类定义 [英] *.h or *.hpp for your class definitions

查看:10
本文介绍了*.h 或 *.hpp 用于您的类定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直使用 *.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.

这篇关于*.h 或 *.hpp 用于您的类定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆