Subj : Re: enumerated variables - how to coerce them to anything else than an Int ? To : borland.public.cpp.borlandcpp From : John F\(dot\) Date : Tue Oct 11 2005 09:26 pm "R.Wieser" wrote: > John F(dot) wrote: > >> before you try any hints, read till the end and decide then what you want > to >> try... :-) > > Well, that sounds as a sane idea :-) I usually answer questions top down without reading further in the first place that leads to another run through the text if sth is covered later on... :-) > [snip] >> Then you are out of luck with that compiler... not possible at all. > > Drats ! That does it ! I'm going to switch to Pascal, or something else. > *Surely* that language can do everything I can imagine ? :-)) So I guess you are not very used to the deeper concepts of C...? C can do everything. Yes. You may write your own compiler supporting longs in enums ;-) [thats worst worst case... actually I'd rather quit my job] BTW :-) Does Pascal have sth like enums? :o) > [snip] >> It does happen. One usually writes > > Well, my documentation indicated that the first value would allways be > Zero, > and the other elements would auto-increment (but can be overridden). Is > there a (good) reason to explicitily define the value of first enumerator > ? You don't need to _explicitely_ assign 0. it doesn't change generated code. It just makes it clearer since you can do typedef enum weird_enum_e { ZERO, //autoenumerated to 0 X = 7, Y, //autoenumerated to 8 Z = 4, A, //autoenumerated to 5 B, //6 C //7 }weird_enum_t; Or if you have a very long list of members... It helps one reading without having to think. It somehow shows that one might have intended to use the autoenumeraton to include values that might be used in calculations (I watch for that more closely if i see it defined explicitely). if you just want names you don't have to care about which valies are assigned to the names. >> If you like to do some "if()s" and "||"s or "&&"s you should >> use 0 for _no_. > > Sorry, it's not my choice. Testing revealed that "default" was > represented > by the value 0, and No and Yes by 1 and 2 ok, then you should stay compatible but here I'd assign these values explicitely [SNIP] > [quote] >> Preprocessing? > > That was, after just plainly putting values into the different fields, my > first idea. But than I remembered C++ having enumerations, which have a > very interresting feature : they can only be used with the variable they > are > "designed" for, something other methods (defines, constants, equates) do > not > have. C has them too... Thats called typechecking or typesafety... Its not forbidden to keep things in mind. The only person that will see them is the developer anyway... > [snip] >> > The only thing I *could* do is to split a number of fields of >> > the type Long into two fields of the type Int. >> No good idea. No portability - little/big endian are looking >> over the shoulder here... > > Would that not allso mean that unions cannot be used either ? Or would > the > (smaller) Int be aligned differently depending on the endianness ? I > might > be wrong, but I don't think so ... Its implementation-defined (that is it depends on the compiler). The problem rather is that you need longs and not ints. Is this DEFINITELY the case? I'd need more information on the whole system: Who fills the structure, who reads it? Is it filled from a file, a binary one? Who is allowed to change it? User, Developer? How are the functions taking it as an argument (a reference to the structure as an argument) defined ? What is done in that functions? [snip] > Thanks for your suggestions, but I'm afraid those have serious drawbacks > (as > have my own solutions). Pre-processing seem to be more work than it's > worth #Definitely not. (you might use a more dirty structure i provided the "easier to maintain" way...) Its the only help that you have left in your tight C/C++ world. One usually utilizes it exensively. I'd even consider a complete redesign of the module (thats usually better and cheaper than 4 or 5 days of debugging...)! regards John .