HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppCritical

*.h or *.hpp for your C++ headers / class definitions

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
headersyourclassfordefinitionshpp

Problem

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.

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

Solution

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

  • 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.



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.

Context

Stack Overflow Q#152555, score: 760

Revisions (0)

No revisions yet.