Link to home
Start Free TrialLog in
Avatar of Sandra-24
Sandra-24

asked on

Can you use a floating point value in an enum?

Building a big project so I can't test the theory atm. Has anyone tried it?

enum foo {NEWFOUNDLAND_STANDARD_TIME = -3.5}

if not what's a good workaround?

-Sandra
ASKER CERTIFIED SOLUTION
Avatar of imladris
imladris
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nesrohtretep
nesrohtretep

Alternatively, use defines  - they can be any type you want:

#define NEWFOUNDLAND_STANDARD_TIME -3.5

 - Peter
I would recommend that instead of defines, use

const float NEWFOUNDLAND_STANDARD_TIME = -3.5;



From Effective C++ 2nd Edition (Item1)

This Item might better be called "prefer the compiler to the preprocessor," because #define is often treated as if it's not part of the language per se. That's one of its problems.

When you do something like this,

#define ASPECT_RATIO 1.653

the symbolic name ASPECT_RATIO may never be seen by compilers; it may be removed by the preprocessor before the source code ever gets to a compiler.
As a result, the name ASPECT_RATIO may not get entered into the symbol table. This can be confusing if you get an error during compilation involving the use of the constant, because the error message may refer to 1.653, not ASPECT_RATIO.
If ASPECT_RATIO was defined in a header file you didn't write, you'd then have no idea where that 1.653 came from, and you'd probably waste time tracking it down. This problem can also crop up in a symbolic debugger, because, again, the name you're programming with may not be in the symbol table.

The solution to this sorry scenario is simple and succinct. Instead of using a preprocessor macro, define a constant:

const double ASPECT_RATIO = 1.653;


Hope this helps

Tincho
Avatar of Sandra-24

ASKER

Not to mention defines make a mess of the global namespace