Gianni Mariani wrote:[color=blue]
> kelvSYC wrote:
>[color=green]
>> Is there an efficient way to check whether an enumerator exists when
>> casting an integer into an enumerated value?
>>
>> For example, say I have this:
>>
>> enum MyEnum {
>> a = 1,
>> b = 2,
>> // more arbitrary constants
>> c = 1000
>> }
>>
>> Is there a way to create a function that would return true if 1 was
>> entered, while (assuming 3 is not the value of a constant in MyEnum)
>> entering 3 would return false?[/color]
>
>
>
> There is an alternative to enums that might be interesting to you.
>
> If you boil down to what is represented by an enum, then it is easily
> represented by a class. If you take this to the extreme level, then
> enums are evil and you should represent all enums with a class.
>
>
> class Color; // akin to enum
> extern const Color & white; // different elements in the enum
> extern const Color & black; // different elements in the enum
>
> //
> // operations you want to do on the enum
> //
> int OrdOf( const Color & );
> int IsInSet( int );
>
> inline bool operator==( const Color & b, const Color & a )
> {
> return &b == &a;
> }[/color]
Shouldn't this be:
return b == a;
Your comparing the locations of the two. I believe you want
to compare by value. Two instances may have the same value
but reside in different locations.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq:
http://www.parashift.com/c++-faq-lite
C Faq:
http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library