Re: enum question.



CBFalconer wrote:
Ian Collins wrote:

CBFalconer wrote:

At_sea_with_C wrote:

.... snip ...


Why is the assignmnt with SOMEVAL allowed? I thought an enum type
was only allowed to have values specified in the enumeration list.
Why is compiler not even warning about it? Am I wrong?

Stronger typed languages will catch it, but not C. In C an enum
just defines an integer type, with some specific named values.
This is basically the price you pay for allowing any disconnected
enum values, e.g something like 0,1,2, 10, 100.

I don't think the range has anything to do with it. C++ enums can
have disconnected values and still enforce valid assignments.


C++ doesn't care how much code it generates. Think about the run
time code needed to do the checking (it can't be done at compile
time, because the values are ints and can be the results of
expressions or function calls).

Sorry, but it is. You can't assign an int to an enum variable without a
cast in C++. There isn't any runtime checking, so you can assign
bollocks values if you go out of your way to do so.

--
Ian Collins.
.



Relevant Pages

  • Re: Idea for ECMA/C# Standard - compile time hash for performance
    ... I agree with you the chance of a compiler change is slim, ... and then delegating to the standard hash for fields accessed less frequently. ... or the array lookup which would require the ... > 64-bit architecture) for each enum value that doesn't map to anything. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: enum as char ?
    ... > to enum exactly besauce i want the compiler ... > to produce a warning on incomplete switch statements. ... Then you can use the 16-bit enum type for your switch statements: ...
    (comp.lang.c)
  • Re: enum question.
    ... snip ... ... Why is compiler not even warning about it? ... In C an enum ...
    (comp.lang.c)
  • Re: Problems with enums across a dll interface?
    ... > on compiler settings the size of an enum could change and lead to memory ... the enum size could change. ... You need to be careful when adding enumerators. ...
    (comp.lang.cpp)
  • RE: passing enum value as an argument
    ... > hours after your only previous reference to Option Strict? ... indeed you would have an Enum, and you WOULD have to use the same type on the ... the compiler would force you to ... >> Public sub New ...
    (microsoft.public.dotnet.general)