enum type in C++
This works:
enum TPriority
{
EPriorityIdle = -100,
EPriorityLow = -20,
EPriorityStandard = 0,
EPriorityUserInput = 10,
EPriorityHigh = 20
};
TPriorit开发者_如何学JAVAy priority = EPriorityIdle;
But this doesn't work:
TPriority priority = -100;
Any reason?
It works too, but you need explicit type
TPriority priority = (TPriority)-100;
shortly put: it defeats the purpose of having an enum
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);
There is no type conversion from the values of an enum type to the enum type itself. Only the other way around.
精彩评论