1

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?

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
Rubens
  • 13,469
  • 10
  • 56
  • 90

3 Answers3

2

If you mean in the header file, no, there is not a good, portable way to do this. It is standard to keep all of your header files in the format -

#ifndef MYHEADER_HPP
#define MYHEADER_HPP

// ... content

#endif

Any other way would be nonstandard and not real C++.

pyrospade
  • 7,124
  • 1
  • 32
  • 50
2

You can always use #pragma once.

Anuraag
  • 76
  • 2
1

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.

Community
  • 1
  • 1
Qortex
  • 5,389
  • 2
  • 30
  • 55
  • I agree they're used by very professional people -- I've seen kernel code speaking `ifndef`'s once I had to open it ^^ It's just I'm used to avoid using it for too small applications, as the one I'm developing. Guess I'll have to accept the facts, anyway (: Thank you all for the answers! – Rubens Feb 26 '13 at 04:52
  • 1
    @Rubens For a *really* small application, there's no requirement to use a header at all. But for now, header guards are how C++ achieves this kind of modularity. – Potatoswatter Feb 26 '13 at 04:56
  • My way of doing things is: keep the included headers as low as possible to improve compilation time, but always put safe-guards not to care about double nested includes. – Qortex Feb 26 '13 at 04:56
  • @Mic This *not to care about ...* is exactly the reason why I both avoid this and I very often just compile my code after writing the whole thing. I go on playing lottery if I don't do so, as I let the compiler mind both my code and it's execution ^^ – Rubens Feb 26 '13 at 05:00