1

I have a .h file which contains several class definitions. I'd like to use C++'s include guards in this file; however, I was wondering which way of using the include guards is considered proper/correct?

One guard protecting everything

#ifndef FOO_BAR
#define FOO_BAR

class Foo
{
};

class Bar
{      
};

#endif

or multiple separate guards.

#ifndef FOO
#define FOO

class Foo
{
};

#endif

#ifndef BAR
#define BAR

class Bar
{      
};

#endif
rectangletangle
  • 42,965
  • 86
  • 186
  • 264
  • 4
    One is enough. Both classes should only be defined once, which happens to anything inside the guard. – chris Sep 08 '12 at 00:32

2 Answers2

5

They are include guards, preventing double inclusion of files. So they should be defined once per file, not per class or function or whatever.

Jost
  • 5,992
  • 8
  • 37
  • 65
2

Have you considered to use #pragma once ? Most modern compilers support it.

Is #pragma once a safe include guard?

Community
  • 1
  • 1
tzuchien.chiu
  • 690
  • 6
  • 11