 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
masi Guest
|
Posted: Wed Feb 15, 2006 3:03 pm Post subject: number of elements in a enum |
|
|
Is there a way to know, programmatically, the number of the elements in an
enumeration, id est:
enum {first, second , third};
how can I know that the enumeration has three elements?
I usually do the following:
enum {first, second , third, number_of_elements};
so I know that number_of_elements contains the number of the elements in the
enumeration.
Thanks,
masi urbano |
|
| Back to top |
|
 |
Thomas Maeder [TeamB] Guest
|
Posted: Wed Feb 15, 2006 3:03 pm Post subject: Re: number of elements in a enum |
|
|
"masi" <masiurb (AT) tin (DOT) it> writes:
| Quote: | Is there a way to know, programmatically, the number of the elements
in an enumeration, id est:
enum {first, second , third};
how can I know that the enumeration has three elements?
|
By looking at the definition :-)
| Quote: | I usually do the following:
enum {first, second , third, number_of_elements};
so I know that number_of_elements contains the number of the
elements in the enumeration.
|
So do I. Is there a problem with this approach? |
|
| Back to top |
|
 |
Liz Albin Guest
|
Posted: Wed Feb 15, 2006 3:03 pm Post subject: Re: number of elements in a enum |
|
|
On Wed, 15 Feb 2006 15:10:00 +0100, masi wrote:
| Quote: | Is there a way to know, programmatically, the number of the elements in an
enumeration, id est:
|
Why do you want to know? There may be some other construct that helps
you do what you'd like.
--
liz |
|
| Back to top |
|
 |
Alan Bellingham Guest
|
Posted: Wed Feb 15, 2006 3:03 pm Post subject: Re: number of elements in a enum |
|
|
"masi" <masiurb (AT) tin (DOT) it> wrote:
| Quote: | Is there a way to know, programmatically, the number of the elements in an
enumeration, id est:
enum {first, second , third};
|
You can't. An enumeration doesn't contain elements - it declares items.
There is no detectable difference between
enum {first, second , third};
and
enum {first = 0};
enum {second = first+1};
enum {third = second+1};
| Quote: | how can I know that the enumeration has three elements?
I usually do the following:
enum {first, second, third, number_of_elements};
so I know that number_of_elements contains the number of the elements in the
enumeration.
|
That fails totally the moment you go
enum {first = -3, second, third, number_of_elements};
as the final value ends up being 0!
But, so long as you let the enumeration start from zero, and do not
explicitly set any of the other members, then the method you are using
is the best I know.
Alan Bellingham
--
ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK |
|
| Back to top |
|
 |
masi Guest
|
Posted: Wed Feb 15, 2006 4:03 pm Post subject: Re: number of elements in a enum |
|
|
Thanks to all.
My idea was to avoid things as:
#define SOMEWHAT 0
#define SOMEWHATELSE 1
#define SOMEWHATXXX 2
#define NUM_CONSTS 3
I have arrays declared as
int vect[NUM_CONSTS];
and I usually iterate on them doing:
for (int i=0; i<NUM_CONSTS; i++){
DoSomeThingWith(vect[i]);
}
but: everytime I had to suppress or add a #define I have to rearrange many
of the elements, that's why the idea of using:
enum{
SOMEWHAT,
SOMEWHATELSE,
SOMEWHATXXX,
NUM_CONSTS
};
thanks again
masi urbano |
|
| Back to top |
|
 |
Liz Albin Guest
|
Posted: Wed Feb 15, 2006 4:03 pm Post subject: Re: number of elements in a enum |
|
|
On Wed, 15 Feb 2006 16:03:00 +0100, masi wrote:
| Quote: | int vect[NUM_CONSTS];
and I usually iterate on them doing:
for (int i=0; i<NUM_CONSTS; i++){
DoSomeThingWith(vect[i]);
}
|
Perhaps you should look at std::vector.
--
liz |
|
| Back to top |
|
 |
Chris Uzdavinis (TeamB) Guest
|
Posted: Wed Feb 15, 2006 4:03 pm Post subject: Re: number of elements in a enum |
|
|
"masi" <masiurb (AT) tin (DOT) it> writes:
| Quote: | Is there a way to know, programmatically, the number of the elements in an
enumeration, id est:
enum {first, second , third};
how can I know that the enumeration has three elements?
|
In the general case, you simply cannot. You're declaring enumerators
to have specific values. In the above example, the values start at
zero and count up sequentially from there.
| Quote: | I usually do the following:
enum {first, second , third, number_of_elements};
so I know that number_of_elements contains the number of the elements in the
enumeration.
|
That's the only way to do it, except that it depends on the fact that
you do not change the values of the enumerators.
--
Chris (TeamB); |
|
| Back to top |
|
 |
masi Guest
|
Posted: Wed Feb 15, 2006 5:03 pm Post subject: Re: number of elements in a enum |
|
|
| Quote: | int vect[NUM_CONSTS];
and I usually iterate on them doing:
for (int i=0; i<NUM_CONSTS; i++){
DoSomeThingWith(vect[i]);
}
Perhaps you should look at std::vector.
|
Which are the advantages in using std::vector?
masi urbano |
|
| Back to top |
|
 |
Chris Uzdavinis (TeamB) Guest
|
Posted: Wed Feb 15, 2006 5:03 pm Post subject: Re: number of elements in a enum |
|
|
"masi" <masiurb (AT) tin (DOT) it> writes:
| Quote: | ok, but why if I do:
typedef enum{
SOMEWHAT,
SOMEWHATELSE,
SOMEWHATXXX,
}ENUMERATION;
I can't access the number of elements? It is an information available to the
compiler!
I'd like a thing like, say, sizeof(ENUMERATION).
|
Sizeof(ENUMERATION) will tell you the storage requirement to hold a
single value. What you're really asking for is not part of the
language, and you have to simulate it within the rules that the
language does offer.
--
Chris (TeamB); |
|
| Back to top |
|
 |
masi Guest
|
Posted: Wed Feb 15, 2006 5:03 pm Post subject: Re: number of elements in a enum |
|
|
ok, but why if I do:
typedef enum{
SOMEWHAT,
SOMEWHATELSE,
SOMEWHATXXX,
}ENUMERATION;
I can't access the number of elements? It is an information available to the
compiler!
I'd like a thing like, say, sizeof(ENUMERATION).
merci |
|
| Back to top |
|
 |
Vladimir Stefanovic Guest
|
Posted: Wed Feb 15, 2006 5:03 pm Post subject: Re: number of elements in a enum |
|
|
| Quote: | My idea was to avoid things as:
#define SOMEWHAT 0
#define SOMEWHATELSE 1
#define SOMEWHATXXX 2
#define NUM_CONSTS 3
|
Besides using std::vector you may think of:
// for string elements ...
AnsiString StringElements[] = { "One", "Two", "Three", "Four" };
int StringElementsCount = ( sizeof( StringElements ) / sizeof(
AnsiString ) );
ShowMessage( StringElementsCount );
// for integer elements ...
int IntegerElements[] = { 1, 2, 3, 4 };
int IntegerElementsCount = ( sizeof( IntegerElements ) / sizeof(
int ) );
ShowMessage( IntegerElementsCount );
--
Best regards,
Vladimir Stefanovic |
|
| Back to top |
|
 |
Gillmer J. Derge [TeamB] Guest
|
Posted: Wed Feb 15, 2006 6:03 pm Post subject: Re: number of elements in a enum |
|
|
Chris Uzdavinis (TeamB) wrote:
| Quote: | What you're really asking for is not part of the
language, and you have to simulate it within the rules that the
language does offer.
|
You and Thomas made that integer-like template a while back. Could that
be extended and/or adapted to make an enum-like type that solves some of
the common gripes about enums? For example, I'm thinking of:
* easy conversion to and from the symbolic name
* get a count or maximum value
* easily create flag values defined as powers of 2 without manually
specifying the values
What, if any, features do real enums support that a class like this
wouldn't?
--
Gillmer J. Derge [TeamB] |
|
| Back to top |
|
 |
masi Guest
|
Posted: Wed Feb 15, 2006 6:03 pm Post subject: Re: number of elements in a enum |
|
|
| Quote: | * easily create flag values defined as powers of 2 without manually
specifying the values
|
that would be very interesting! |
|
| Back to top |
|
 |
Rudy Velthuis [TeamB] Guest
|
Posted: Wed Feb 15, 2006 7:03 pm Post subject: Re: number of elements in a enum |
|
|
At 15:38:39, 15.02.2006, Alan Bellingham wrote:
| Quote: | "masi" <masiurb (AT) tin (DOT) it> wrote:
Is there a way to know, programmatically, the number of the elements
in an enumeration, id est:
enum {first, second , third};
You can't. An enumeration doesn't contain elements - it declares items.
|
An enumeration does EXACTLY the same in Delphi (although it is stricter -
they are not assignment compatible with integers and other ordinal
types), and yet I can specify Low(TheEnumType) and High(TheEnumType),
where the compiler will return, as plain literals, first and third,
respectively.
I guess there is no such thing in C++?
--
Rudy Velthuis [TeamB] http://rvelthuis.de/
"It's not that I'm afraid to die, I just don't want to be there
when it happens." -- Woody Allen, From 'Death' 1975. |
|
| Back to top |
|
 |
Alan Bellingham Guest
|
Posted: Wed Feb 15, 2006 7:03 pm Post subject: Re: number of elements in a enum |
|
|
"Rudy Velthuis [TeamB]" <velthuis (AT) gmail (DOT) com> wrote:
| Quote: | You can't. An enumeration doesn't contain elements - it declares items.
An enumeration does EXACTLY the same in Delphi (although it is stricter -
they are not assignment compatible with integers and other ordinal
types), and yet I can specify Low(TheEnumType) and High(TheEnumType),
where the compiler will return, as plain literals, first and third,
respectively.
I guess there is no such thing in C++?
|
No, because C (and thus C++) are not as you describe. In C, with the
examples previously given, you'd have to use Low() and High() ... there
is no name for the enumeration. A little bit difficult, I think you'd
agree.
In many ways, it'd be nice to have a better type of enum, but then it'd
then fail by not being "assignment compatible with integers and other
ordinal types)".
It's the very flexibility in C (and C++) that also brings certain
shortcomings. The ability to use drop almost any old values into an
enumeration type is much of what makes it so powerful, but that same
power also means you can't actually really know what the maximum value
will be.
For example:
enum Colour { Black = 0, Red = 1, Green = 2, Blue = 4 };
Colour c = (Red | Green | Blue);
gives a legal enumeration type that contains a valid value higher than
the highest value actually enumerated. Sure, we could do:
enum Colour { Black = 0, Red = 1, Green = 2, ..., Blue = 4, ...,
White = 7 };
but it tends to get very messy very fast once you go beyond a tiny
handful of bits.
Alan Bellingham
--
ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|