Solved

Can you use a floating point value in an enum?

Posted on 2003-10-27
4
184 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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

21 Experts available now in Live!

Get 1:1 Help Now