C++ best way to define cross-file constants

情到浓时终转凉″ 提交于 2019-12-04 09:23:23

问题


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


回答1:


Get rid of the extern and you're set.

This code works perfectly fine in a header, because everything is "truly constant" and therefore has internal linkage:

const int BEGINNING_HEALTH = 10;
const int BEGINNING_MANA = 5;
const char BEGINNING_NAME[] = "Fred";
const char *const BEGINNING_NAME2 = "Barney";

This code cannot safely be put in a header file because each line has external linkage (either explicitly or because of not being truly constant):

extern const int BEGINNING_HEALTH = 10;
extern const int BEGINNING_MANA = 5;
const char *BEGINNING_NAME = "Wilma";  // the characters are const, but the pointer isn't



回答2:


How about enums?

constants.hpp

  enum {
    BEGINNING_HEALTH = 10,
    BEGINNING_MANA = 5
  }



回答3:


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




回答4:


make use of namespaces:

namespace GameBeginning {
    const int HEALTH = 10;
    const int MANA   = 5; 
};

then u can use as player.health = GameBeginning::HEALTH;




回答5:


Most compilers simply don't allocate space for const POD values. They optimize them out and treat them as if they had been #defined, don't they?




回答6:


What ever happened to a simple:

#define BEGINNING_HEALTH 10

Man, those were the days.
Oh wait, those still are the days!




回答7:


perhaps something along the lines of a static class?

class CONSTANTS {
public:
static inline int getMana() { return 10;};
};



回答8:


As a quick answer to the title question, a singleton pattern is a possible best, C++ way to define cross-file constants and insure only one instance of the object.

As far as the template parameter problem, you need to pass a type not a value. Your type is "int".



来源:https://stackoverflow.com/questions/641397/c-best-way-to-define-cross-file-constants

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!