使用 .hpp 或 .h 文件在 C++ 中有什么区别以及影响性能的因素是什么?

huangapple go评论101阅读模式
英文:

What is the difference and what affects the performance of using .hpp or .h files in c++?

问题

我正在学习C++中的模板,但在书中它们使用.hpp文件,而在我看过的一些视频中,模板被放在.h文件中。然而,在书中它使用.hpp,但没有解释原因和好处,我想了解为什么?

使用.hpp或.h文件在C++中有什么区别,以及使用哪种文件会影响性能?能否帮助我解答这个问题?谢谢。

英文:

I am studying the templates in c++ but in the book they use .hpp files and in the videos I have seen the templates are placed in .h files in some videos I have watched however in the book it uses .hpp it does not explain why and the benefits, I would like to understand why ?

What is the difference and what affects the performance of using .hpp or .h files in c++? Could you please help me. Thanks

答案1

得分: 2

没有区别,它们只是不同的名称。包含文件由预处理器处理,它不关心你的文件叫什么名字,你可以以.blub.ramons_special_header结束你的头文件,这对编译器没有影响(从技术上讲,对于其他源文件也是如此,你可以将C++源代码放在.c文件中,但支持多种不同语言模式的编译器如gcc可能会根据文件名默认改变行为,但大多数编译器都有设置来覆盖这种行为)。

人们使用不同名称的唯一原因是因为在某些项目中,某些名称用于特定的目的(例如,在一些混合C和C++源文件的项目中,.hpp表示"C++头文件",.h表示"C头文件"),或者因为有些人配置了他们的编辑器,根据文件名来不同对待某些文件。除此之外,这只是个人偏好的问题。有些人使用文件扩展名如.hh.hxx.H,如果你搜索一下,你会发现有关哪个名称最好的激烈讨论。

英文:

There's no difference, they're just different names. Includes are handled by the preprocessor, and it doesn't care what names your files have, you can end your headers in .blub or .ramons_special_header and it'd make no difference to the compiler (technically speaking the same is true of other source files as well, you could put C++ source code in a .c file, but there compilers that support multiple different language modes like gcc may change how they behave by default depending on what you name your files, but most compilers will have settings to override that behaviour anyhow).

The only reason people use different names is because some names are used for special purposes in certain projects (e.g. .hpp meaning "C++ header" and .h meaning "C header" in some projects with mixed C and C++ sources), or because some people have their editors configured to treat certain files differently based on name. Beyond that it's just a matter of preference. Some people use file endings like .hh or .hxx or .H, and if you search for it you'll find heated discussions about which name is best.

huangapple
  • 本文由 发表于 2023年7月20日 20:51:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730103.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定