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 02:08 am Hi Rudy! before you try any hints, read till the end and decide then what you want to try... :-) "R.Wieser" wrote: > John F(dot) wrote: > [snip] >> Try including a value larger than maximum int. > > I just tried it. Guess what : the compiler complained about the value not > fitting in an Int. :-\ Then you are out of luck with that compiler... not possible at all. >> OK so far. what are the values for def, y, n ? > > My intention (with the declaration I showed) was to give them the values > 0, > 1 and 2 (although I did not yet check if that really happens). It does happen. One usually writes enum { first_element = 0, //explicitly second, //first_element+1 = 1 third //second +1 = 2 }; so that it is clear where it starts... Usually 0 is considered false and anything different from 0 is considered true. If you like to do some "if()s" and "||"s or "&&"s you should use 0 for _no_. default should be 0,1 or 2? then it can not be part of the enum. enums are fixed. >> and why weren't they designed to be enums then? :-) > > They probably where .... But the structure I've currently got is only a > recreation, not the origional. I made it by looking how changes settings > in > a program changed the stored data. Yep, a bit of reverse engeneering :-) > > [snip] >> Are you allowed to change the structure??? How comes that? > > It's an old program for which I want to be able to create different > setups, > without manually having to select them in the program itself. A > template-generator if you will. Preprocessing? #define PORT_DEFAULT -1 #define PORT_COM1 0 #define PORT_COM2 1 #define PORT_LPT1 2 .... #define PORT_USED (PORT_DEFAULT) .... >> Do you still need to store arbitrary longs into these member-variables? > > Alas, yes. I can't change anything about the program itself. OK. > The only thing I *could* do is to split a number of fields of the type > Long > into two fields of the type Int. That would most-of-the-time work, as > most > enumerations are done with small positive numbers. No good idea. No portability - little/big endian are looking over the shoulder here... > But I would like to find a method that is of more generic use (for Ints as > wel as other types). Unions? union { long MyLong, enum E MyEnum }X; X.E=default; X.MyLong=1234567; >> Could you give a short snippet of the code you need to be done? > > Well, currently it's just a structure with some fields. And some of those > fields only may have (the numerical equivalent) of certain settings (like > "default", "yes" or "no"). > > struct { > UseLpt=default; -- default, com1, com2, lpt1, lpt2, lpt3 > ComHandshake=none; -- none, hardware, xonxoff > Baudrate=9600; -- 50, 75, 110, 150, 1200, 2400, 9600 > -- etc. > } Settings; that is the job for plain enums... or preprocessing. there will hardly ever be 2^32 ports... (as long as i'll be still alive...) Baudrate could be long (115000 e.g.) In that case I'd use preprocessing... set_vals.hxx: #define PORT_LPT1 1 .... settings.hxx: #include set_vals.hxx #define PORT_USED (PORT_LPT1) .... program.cxx struct ... UseLPT = PORT_USED; //settings.h .... it doesn't perform typechecking but it works... regards John .