Re: Iterating through an enum???
From: Simon Trew (noneofyour_at_business.guv)
Date: 04/29/04
- Next message: Simon Trew: "Re: dynamic_cast (problem with)"
- Previous message: Roger: "Re: Passing variable number of arguments as references."
- In reply to: muchan: "Re: Iterating through an enum???"
- Next in thread: muchan: "Re: Iterating through an enum???"
- Reply: muchan: "Re: Iterating through an enum???"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 29 Apr 2004 09:13:20 +0100
"muchan" <usenet@usenet.usenet> wrote in message
news:QHMjc.1714$37.248502@news.siol.net...
>
> Whatever the specification is, and whatever is theoretically possible,
> I stand that "enum" stands for "enumeration", so I'd recommend (myself)
> to enumerate all the possible value in the definition, either used or
> not used in a concrete program.
Well, if there are say 10 "base" values in an enum, each representing a
one-bit flag, then there would be 2^10 = 1024 possible values of the
enumeration, few of which are likely to be used. It seems a bit of overkill
to have to name and specify them.
Are you just unhappy with the idea of enums representing sets of
flags/values? Unfortunately for whatever reason that has become the norm in
C and C++; this mechanism is almost always preferred to using bitfields, for
example. At least we keep all the values together in a logical grouping (the
enum), although I can see the advantage of defining consts in a namespace
instead (because then you have to refer to the enum values through the
namespace scope, like in C++).
> That is, if to values are combined (that is, |ed ) to make combined value,
> it should be in enumeration list,
> like:
> enum { a = 1, b, c, d, e = 10, ae = (a|e), be, ce, de };
> ha! I don't know if such use of a and e is allowd in enum declaration...
It is. (You could have tried it.)
> If combination is infinit (well, always finit within the range of int...)
> and/or too many that it isn't practical to enumerate all, I think i should
> use integer instead of enum from the starting. :)
Well it is part of the common-sense definition of an enumeration that you
can, er, enumerate the items in it, i.e. you can define the contents of the
set by specifiying each of its members rather than having to specify them
with a formula ("set comprehension"). So it seems fair enough that C++
honors this definition by requiring you to enumerate the members of the
enum.
> Or, another (self) guidance would be all the explicite values in enum
> declaration should be defined as constant, either with #define or with
> const int.
I don't know what you think that gains you over defining them in an enum.
> (...and I don't know if const int declaration is treated with compiler
> without allocation in the runtime program...)
>
> so above case
> const int A = 1;
You must be able to get a pointer/reference to A within the program, and to
do that, A would have to be allocated a memory location. But, if you don't
actually get a pointer/reference to A within your program, I think the
compiler is free to get rid of the allocation. i.e. in some programs the
optimizer would be able to replace all uses of A with the const value that A
represents, and then there's no need to allocate memory for A. The optimizer
might not do this if the initialization expression was more complicated than
a simple numeric literal.
I did think that a const delcaration of the form:
const A = 1;
would definitely *not* let you take a pointer/reference to A (you don't know
it's type, for a start). But in VC6 it happily let me take an int* to A.
This might just be a bug.
> I would define entire definition string in macro:
>
> #define ABC_ENUM { a = A, b, c, d, e = E, ae = AE, be, ce, de }
> enum abc ABC_ENUM;
> abc abcarray[] = ABC_ENUM;
Unfortunately the ABC_ENUM is not a valid initialization list for the array.
> Then I'd like to reduce the duplicate memory, so the array declaration
> should be inside the constructor of a class, which wraps the enum from
> global namespace,
>
> ABC::ABC()
> {
> abc abcarray[] = ABC_ENUM; // temporary hold the value in array
> v_abc = std::vector(abcarray); // this is redundant, elements are
copyed twice!
> }
You could just use v_abc.push_back() to put the individual elements in
rather than copy.
for (int i = 0; i < sizeof(abc)/sizeof(abc[0]); ++i)
v_abc.push_back(abc[i]);
- Next message: Simon Trew: "Re: dynamic_cast (problem with)"
- Previous message: Roger: "Re: Passing variable number of arguments as references."
- In reply to: muchan: "Re: Iterating through an enum???"
- Next in thread: muchan: "Re: Iterating through an enum???"
- Reply: muchan: "Re: Iterating through an enum???"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|