Solved

Can you use a floating point value in an enum?

Posted on 2003-10-27
4
188 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

733 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