[英]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? 任何原因?
它也可以,但是您需要显式类型
TPriority priority = (TPriority)-100;
简而言之:它无法实现枚举的目的
You cannot assign an int to an enum, even if the value matches one of the enum's values. 即使值与枚举值之一匹配,也不能将int赋给枚举。
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. 只是相反。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.