Defining global constant in C++

前端 未结 10 1910
梦如初夏
梦如初夏 2020-11-29 16:44

I want to define a constant in C++ to be visible in several source files. I can imagine the following ways to define it in a header file:

  1. #define GLOBAL_
10条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-29 17:06

    #define GLOBAL_CONST_VAR 0xFF // this is C code not C++
    int GLOBAL_CONST_VAR = 0xFF; // it is not constant and maybe not compilled
    Some function returing the value (e.g. int get_LOBAL_CONST_VAR()) // maybe but exists better desision
    enum { LOBAL_CONST_VAR = 0xFF; } // not needed, endeed, for only one constant (enum elms is a simple int, but with secial enumeration)
    const int GLOBAL_CONST_VAR = 0xFF; // it is the best
    extern const int GLOBAL_CONST_VAR; //some compiller doesn't understand this
    

提交回复
热议问题