我的类定义一直使用*.h文件,但在阅读了一些boost库代码后,我意识到它们都使用*.hpp。我一直很讨厌这个文件扩展名,我想主要是因为我不习惯它。
使用*.hpp而不是*.h的优点和缺点是什么?
我的类定义一直使用*.h文件,但在阅读了一些boost库代码后,我意识到它们都使用*.hpp。我一直很讨厌这个文件扩展名,我想主要是因为我不习惯它。
使用*.hpp而不是*.h的优点和缺点是什么?
当前回答
Codegear c++ Builder使用.hpp作为从Delphi源文件自动生成的头文件,使用.h文件作为“自己的”头文件。
所以,当我写一个c++头文件时,我总是使用.h。
其他回答
幸运的是,这很简单。
如果您使用c++,您应该使用.hpp扩展名,并且您应该使用.h表示C或混合使用C和c++。
使用哪个扩展名并不重要。两种都可以。
我用*.h表示C,用*.hpp表示c++。
c++ ("C Plus Plus")作为.cpp是有意义的
扩展名为.hpp的头文件没有相同的逻辑流。
下面是C和c++头文件命名不同的几个原因:
Automatic code formatting, you might have different guidelines for formatting C and C++ code. If the headers are separated by extension you can set your editor to apply the appropriate formatting automatically Naming, I've been on projects where there were libraries written in C and then wrappers had been implemented in C++. Since the headers usually had similar names, i.e. Feature.h vs Feature.hpp, they were easy to tell apart. Inclusion, maybe your project has more appropriate versions available written in C++ but you are using the C version (see above point). If headers are named after the language they are implemented in you can easily spot all the C-headers and check for C++ versions.
记住,C不是c++,除非你知道你在做什么,否则混合和匹配是非常危险的。适当地命名源代码可以帮助您区分语言。
正如这里许多人已经提到的,我也更喜欢使用.hpp来处理使用模板类/函数的纯头文件库。我更喜欢使用.h作为头文件,附带.cpp源文件或共享或静态库。
我开发的大多数库都是基于模板的,因此只需要头文件,但在编写应用程序时,我倾向于将声明与实现分开,最终使用.h和.cpp文件