1

I have a header file called calendarType.h

Is it appropriate to add

#ifndef calendarType
#define calendarType

and

#endif as the last row?

2 Answers2

2

Yes, those will work to make sure the lines in between #ifdef and #endif are only parsed once during each compilation (even if they get #include'd from more than one location)

The only thing I might do differently is to change calendarType to calendarType_h in each of the first two lines -- that will help make sure the #define'd macro-token doesn't interfere with any class or struct named calendarType.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
1

Those are called include guards. They should be used in most header files to avoid a problem known as double inclusion. Double inclusion will most likely result in failed compilation. From Wikipedia:

If certain C or C++ language constructs are defined twice, the resulting translation unit is invalid.

#pragma once is an alternative to traditional include guards. While not actually part of the C++ standard, it is well supported across compilers. #pragma once is less code to type and less prone to error (e.g. accidentally re-using the same include guard).

noddy
  • 3,827
  • 3
  • 24
  • 21