Solved

Can you use a floating point value in an enum?

Posted on 2003-10-27
4
185 Views
Last Modified: 2010-04-02
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
0
Comment
Question by:Sandra-24
4 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 125 total points
ID: 9629873
No, enums have to be integers.

You could use "scaled" integers. That is, set your enums to the required value times 10. So 10 for 1 hour, 30 for 3 hours and 35 for three and a half hours.

You could then either do everything in "scaled" integers, or, when you need to use them, convert the enum value to a float, and divide by 10.
0
 

Expert Comment

by:nesrohtretep
ID: 9630466
Alternatively, use defines  - they can be any type you want:

#define NEWFOUNDLAND_STANDARD_TIME -3.5

 - Peter
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9630523
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
0
 
LVL 3

Author Comment

by:Sandra-24
ID: 9630627
Not to mention defines make a mess of the global namespace
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now