Defining global constant in C++

前端 未结 10 1896
梦如初夏
梦如初夏 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:05

    It depends on your requirements. (5) is the best for most normal usage, but often results in the constant taking up storage space in every object file. (6) can get around this in situations where it's important.

    (4) is also a decent choice if your priority is guaranteeing that storage space is never allocated, but it only works for integral constants of course.

提交回复
热议问题