S
slot
Is it possible to compare "enum" types to basic types like "unsigned short"
type at all? Thanks!
type at all? Thanks!
slot said:Is it possible to compare "enum" types to basic types like "unsigned short"
type at all? Thanks!
slot said:Is it possible to compare "enum" types to basic types like "unsigned short"
type at all? Thanks!
Howard said:Certainly! That's probaby more common than using a variable whose type is
actually an enum. (At least it is in my code.) A lot of code is written
like this:
// given an enum something like this:
enum { errNone, errNotFound, errRead, errWrite };
// code uses it like this:
int err = DoSomething();
if (errNone == err)
// continue with more stuff
else
// do something with the error value
Victor Bazarov said:What is the advantage of your approach above versus
enum ErrCode { errNone, errNotFound, errRead, errWrite };
...
ErrCode err = DoSomething();
...
? Yes, your 'DoSomething' function now has to return ErrCode, otherwise
the initialisation of 'err' won't compile without an explicit cast, but
isn't that the whole point of type safety?
What if I just add 'errBadCmd' right between 'errNone' and 'errNotFound'?
The whole system suddenly stops working, doesn't it? Not in my case. If
ErrCode is used everywhere, then the module that contains 'DoSomething'
would have to be recompiled, but at least it will keep the values in sync.
Victor
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.