c++ - How can I define a structure of physical constants? -
i define sort of structure (struct? namespace? class?) of physical constants known globally program. purpose of doing allow me give constants simple, intuitive names while protecting values elsewhere. example, define q within structure fundamental charge without having worry accidentally using q loop variable somewhere else in program. thought define struct (in main.h):
struct constants { float q=1.6022e-19; } _c;
but gives me error
main.h:79: error: iso c++ forbids initialization of member 'q' main.h:79: error: making 'q' static main.h:79: error: iso c++ forbids in-class initialization of non-const static member 'q'
i searched both here , internet @ large didn't find answer. if know of one, please redirect me. i'm new both stack overflow , c/c++ appreciate patience.
one solution set of static variables in "constants" namespace:
namespace constants { static double constexpr q = 1.3; }
to refer variable, do:
constants::q
you can define static variables within classes or structs well, if makes sense , keeps code more organized.
Comments
Post a Comment