I have a C++ code that’s a physics simulation tool.
I would like to store some physical constants, conversion factor between different sets of units, and also some more application specific constants (such as definition like enum Planes {X=0, Y=1}) and I would like to be able to access them from everywhere in my code.
What would be the best way to do that ?
I think one way would be to create a namespace namespace constants (which can then be a nested namespace in my main namespace) with nested namespaces (like ‘constants’, ‘units’, etc.).
Would it be the way you would do that ?
If I use that method, do I have to make it a header file and include it everywhere ?
If I understand correctly the variables in the namespace at global scope have static linkage, but no external linkage. Then if I want to use them without including a file, I also have to declare them extern ?
As you can see I am a bit confused about that…
Namespace constants are the way to go in most cases.
Yes, or not everywhere but only where it’s really USED.
Yes, you have to do it that way :