开发者

enum type in C++

开发者 https://www.devze.com 2022-12-30 22:45 出处:网络
This works: enum TPriority { EPriorityIdle = -100, EPriorityLow = -20, EPriorityStandard = 0, EPriorityUserInput = 10,

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消