I often find that I need to define a bunch of physical constants that must be shared by many classes. I have done a little research and I have found the following solutions, however I would like to hear from the experts:
i) Defining all constants as global variables in main and prefixing them with constextern; all implementation files must also include the same list of constants.
ii) Defining a singleton class containing all my constants, instantiating the singleton, and including it in every class that requires access to the constants. Maybe a bit messy.
iii) Defining a namespace containig all my constants. Haven't tried this one yet.
I only program occasionally in C++, so probably my first choice would be #1 above, but not sure if you have the concept down properly.
#1 above requires two files: One CPP and one H. The CPP one would have the global constants:
doubleconst c_pi = 3.14159265359;
While the H one would have the externs:
1 2 3
#pragma once
externdoubleconst c_pi;
Now all you have to do is #include the H file in any CPP or H file that needs constants.
#2 is in my opinion a waste. There is no state information required so an object is just wasted RAM. Declare them statically inside a class or add them to a namespace.