I have a class A
, and two classes, B
and C
, inheriting from A
.
Apart from the methods from A
, each class is to offer different things, and, therefore, I'd like to put them in two different files -- b.hpp
and c.hpp
, since they're templates with inline functions.
I also have a class User
, using both B
and C
classes. In user.cpp
, I should have something close to:
# include "b.hpp"
# include "c.hpp"
class User{
...;
};
Which would raise a compiler redefinition error, for what I'm keeping both classes B
and C
in a bc.hpp
.
The concern here is rather aesthetic other than a programming error: I just don't find very elegant using ifdef / ifndef
directives in small applications -- they always look to me like IDE's solution for people managing too many modules, and for those not even aware of the directives.
Is there any (even nonstandard, but at least sane) workaround for this, or do I have to stick with either having two different classes in the same file and using ifdef / ifndef
?
ifndef
really are good practice. They help you to make your files header self-contained.
You do not want some file A
to stop compiling because you removed an include
statement C
somewhere in another file B
. So for that matter, you include C
in both A
and B
. Maybe A
includes B
, but it doesn't matter. Otherwise, you would have to add an include C
in B
whenever you remove include C
from A
.
So ifndef
really are good practices and should be used. Depending on your compiler, you may have syntactic shortcuts such as pragma once (find a discussion here) - but keep in mind it's not standard even if supported by most compilers.
Btw, professional production code does use ifndef
guards.