You generally shouldn't use e.g. const int
in a header file, if it's included in several source files. That is because then the variables will be defined once per source file (translation units technically speaking) because global const
variables are implicitly static, taking up more memory than required.
You should instead have a special source file, Constants.cpp
that actually defines the variables, and then have the variables declared as extern
in the header file.
Something like this header file:
// Protect against multiple inclusions in the same source file
#ifndef CONSTANTS_H
#define CONSTANTS_H
extern const int CONSTANT_1;
#endif
And this in a source file:
const int CONSTANT_1 = 123;