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

使用*.hpp而不是*.h的优点和缺点是什么?


当前回答

c++ ("C Plus Plus")作为.cpp是有意义的

扩展名为.hpp的头文件没有相同的逻辑流。

其他回答

在Bjarne Stroustrup的《c++程序设计语言第三版》(第nº1本c++必读书籍)中,他使用了*.h。所以我认为最好的做法是使用*.h。

但是,*.hpp也很好!

我使用。h,因为这是微软使用的,也是他们的代码生成器创建的。没必要违背常理。

下面是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++,除非你知道你在做什么,否则混合和匹配是非常危险的。适当地命名源代码可以帮助您区分语言。

幸运的是,这很简单。

如果您使用c++,您应该使用.hpp扩展名,并且您应该使用.h表示C或混合使用C和c++。

c++ ("C Plus Plus")作为.cpp是有意义的

扩展名为.hpp的头文件没有相同的逻辑流。