tags:

views:

121

answers:

4
+1  Q: 

enum type in C++

This works:

enum TPriority 
{
    EPriorityIdle = -100,
    EPriorityLow = -20,
    EPriorityStandard = 0,
    EPriorityUserInput = 10,
    EPriorityHigh = 20
};

TPriority priority = EPriorityIdle; 

But this doesn't work:

TPriority priority = -100;

Any reason?

+8  A: 

It works too, but you need explicit type

TPriority priority = (TPriority)-100;
Victor
+1  A: 

You cannot assign an int to an enum, even if the value matches one of the enum's values.

However, casting will work:

TPriority priority = static_cast<TPriority>(-100);
ThiefMaster
A: 

There is no type conversion from the values of an enum type to the enum type itself. Only the other way around.

pmr
+3  A: 

shortly put: it defeats the purpose of having an enum

Anders K.