How to share global constants with minimum overhead at runtime?

后端 未结 5 549
[愿得一人]
[愿得一人] 2021-01-19 05:21

I am using C++11. I am not allowed to use external libraries like boost etc. I must use STL only.

I have a number of events, which must be identified as string const

5条回答
  •  梦毁少年i
    2021-01-19 06:14

    For the sake of proper abstraction and good design, you should define an event class. This event class will have either:

    • A method which provide a string (e.g. name() or system_name())
    • A conversion operator to a string (not recommended)
    • A to_string() freestanding function which takes such an event (not recommend)

    But beyond that - all of your class can now use an enum, or an index, or whatever they like - they'll just need to use the conversion method whenever they interact with whatever it is that requires strings. Thus none of your classes has to actually know about those strings itself.

    The strings themselves can stay within the .cpp implementation file of the class, and nobody else has to know about them. (unles they are actually defined in code that's not yours, but that's not how you described the problem.)

提交回复
热议问题