Hello everyone. We all know we're never supposed to use "magic numbers" in our code and should instead replace them with named constants to make the code more readable and future changes much easier. The problem is, I can't think of a satisfactory way to do this in C++. The choices I've thought of so far include:
-Use preprocessor #defines, which offer the best readability but have no knowledge of scope and completely violate OO programming principles.
-Use const data inside the class, but this prevents compiler optimizations and unecessarily stores a simple number in memory multiple times.
-Use static const data inside the class with a "get()" function, but this ruins readability (i.e. "if (myObject.getValue() == myClass::getMax())") and seems to me like it would hamper compiler optimizations.
-Use static const data inside the class, but make it public, which improves readability and optimization but violates the "no public data" rule.
None of these options seem to fit the principles of OO programming. Which would you choose? Is there another option that I'm missing here?
Thanks for any assistance.
-Use preprocessor #defines, which offer the best readability but have no knowledge of scope and completely violate OO programming principles.
-Use const data inside the class, but this prevents compiler optimizations and unecessarily stores a simple number in memory multiple times.
-Use static const data inside the class with a "get()" function, but this ruins readability (i.e. "if (myObject.getValue() == myClass::getMax())") and seems to me like it would hamper compiler optimizations.
-Use static const data inside the class, but make it public, which improves readability and optimization but violates the "no public data" rule.
None of these options seem to fit the principles of OO programming. Which would you choose? Is there another option that I'm missing here?
Thanks for any assistance.