Re: smarter enums

From: Aguilar, James (jfa1_at_cec.NOBOTSwustl.edu)
Date: 07/12/04


Date: Mon, 12 Jul 2004 00:11:59 -0400


"Mark A. Gibbs" <x_gibbsmark@rogesr.com_x> wrote in message
news:RWnIc.879359$Ar.376465@twister01.bloor.is.net.cable.rogers.com...
>
> [snip]

I don't have any advice to give you about how to do this, since I am just
learning the language myself. However . . .

I don't think that what you're trying to do is worth your time. My
understanding is that you are trying to design an enum that has some minor
added features over a standard enum. Basically, it's an enum that also
gives you the ability to define class functionality, such as methods and
operators.

But, as I understand it, the whole point of enums is that they aren't
classes, but just ints masked by constant names. They are supposed to be
quick and simple, with no bells and whistles. With C++, they also have
types, but those are simply to guarantee that the user knows what's going
on. AFAIK, you can still throw them around like the ints they represent,
such as:

enum Result { SUCCESS, FAILURE };

int main()
{
  ...
  if (!error()) return SUCCESS;
  else return FAILURE;
}

So my advice, which you might take, even though I'm as new to C++ as the
next guy, is to define classes if you need methods, and let enums be enums.



Relevant Pages

  • Re: Exception vs Boolean
    ... An enum or boolean to indicate success or failure *is* a "return ...
    (microsoft.public.dotnet.general)
  • Re: enum question.
    ... Why is compiler not even warning about it? ... In C an enum ... because the values are ints and can be the results of ...
    (comp.lang.c)
  • Re: enums ... what am I doing wrong?
    ... > I'm casting enums back and forth to ints so to use them as an index ... How about using constants instead of enum values? ... same readability of enum ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Custom values in Enum
    ... The only reason to use byte or short in or out of an enum is if you are going to hold a big collection or matrix of them and you want to relieve memory pressure. ... The system architecture is generally most efficient in terms of access speed, with ints. ... Is there a way to declare an Enum with non-secventual values? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Enum type check in function call
    ... Got a function that takes in a enumerated type. ... 'g++' generates a error msg for the same. ... The /constants/ are just ints, but the `enum wossname`s aren't; ...
    (comp.lang.c)