Alex Rivera | Logout

C++ best way to define cross-file constants

Asked 2009-03-13T03:45:27.830
12

I am working on a game and have an interesting question. I have some game-wide constant values that I want to implement in one file. Right now I have something like this:

constants.cpp

extern const int BEGINNING_HEALTH = 10;
extern const int BEGINNING_MANA = 5;

constants.hpp

extern const int BEGINNING_HEALTH;
extern const int BEGINNING_MANA;

And then files just #include "constants.hpp" This was working great, until I needed to use one of the constants as a template parameter, because externally-linked constants are not valid template parameters. So my question is, what is the best way to implement these constants? I am afraid that simply putting the constants in a header file will cause them to be defined in each translation unit. And I don't want to use macros.

Thanks

Edit
Report

1 Answer

8

Use "static const int" in your .hpp file, and put nothing in the .cpp file (except whatever other code you have there of course).

answered 2009-03-13T04:33:53.893

Your Answer