I know that the standard libraries use includes with .h for the old C libraries and includes without .h for the up to date libraries. But what is the better practice for one's own classes?
Where I work we always had an include folder with two files per class: classname.h and ClassName. ClassName includes the classname.h and classname.h includes the real class header. To include a class you then use
#include <ClassName>
That's the way Qt does it, and I'm pretty sure Qt is the reason they started doing it in my company. But are there any benefits of that?
The drawbacks are pretty obvious, I think:
Do you use this style? If so, why? What are arguments for it? Are there more arguments against it?
Vitamin C, also known as ascorbic acid, is necessary for the growth, development and repair of all body tissues. It's involved in many body functions, including formation of collagen, absorption of iron, the proper functioning of the immune system, wound healing, and the maintenance of cartilage, bones, and teeth.
Vitamin C is water-soluble, so any excess is usually excreted in the urine rather than stored in the body. It's safe in almost any amount from foods, and supplements in recommended amounts are also regarded as safe for most people.
Power-C is a supplement that is infused with powerful extracts from: Moringa Extract – has many important vitamins and minerals. The leaves have 7 times more vitamin C than oranges and 15 times more potassium than bananas. It also has calcium, protein, iron, and amino acids, which help your body heal and build muscle.
First, I'm going to make up a term and call it "Class-Specific Include File" or "CSIF". These files do not end in .h
, even though they are used like a header, thus the "include file" part. Since I'm going to keep referring to the thing, I might as well give it an acronym, too.
Qt has a "header" file with no extension for each defined Qt class
or struct
, known as CSIF for this answer. CSIFs go right into the \include
path with normal header files, but they all do the same thing if you open one up. Let's start with with include files in QtWidgets
, particularly the ones related to QStyleOption
:
\include
...
QStyleOption
qstyleoption.h
QStyleOptionButton
QStyleOptionComboBox
...
If you open up one of those QStyleOption
-related CSIFs, all contain one line of code:
#include "qstyleoption.h"
And in qstyleoption.h
:
#include "../../src/widgets/styles/qstyleoption.h"
All that the CSIF does is #include
the correct header from the source tree for the class it is named after. It creates a level of abstraction between the actual definition of the class and #include
statement needed to use it. As a result, this code:
//In some .cpp file somewhere
#include <QStyleOptionButton>
#include <QStyleOptionComboBox>
includes the same header file (protected by guards, of course), but the developer using Qt doesn't need to know that. All the developer cares about is that s/he's using a class, and s/he wants to be certain that the class is available. I don't know if this was the original logic, but that's the effect it has.
1. Do you use this style? If so, why?
Yes, but only because the project I'm working on emulates the Qt library style, so that Qt developers can use it comfortably alongside Qt. Otherwise, [insert profuse swearing].
2. What are arguments for it?
The only good argument I know of for it is above, in how Qt uses it to abstract-away classes and the location of their definitions. As a user of Qt, ensuring I have the proper header files included is a breeze. If I use class QFoo
, I include: <QFoo>
. Simple.
3. Are there more arguments against it?
There are plenty of arguments against it, as you noted in your question.
I'm sure others can come up with more examples of CSIF making life difficult. In all, one must weigh the balance of the increased rigidity it creates in your library and the benefits to the end-developer before making a decision.
As for how your workplace uses it... I'm shrugging. Someone copied the Qt style without understanding why, perhaps?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With