C++ 中的枚举类型
这有效:
enum TPriority
{
EPriorityIdle = -100,
EPriorityLow = -20,
EPriorityStandard = 0,
EPriorityUserInput = 10,
EPriorityHigh = 20
};
TPriority priority = EPriorityIdle;
但这不起作用:
TPriority priority = -100;
有什么原因吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它也有效,但你需要显式类型
It works too, but you need explicit type
简而言之:它违背了枚举的目的
shortly put: it defeats the purpose of having an enum
即使该值与枚举的值之一匹配,也不能将 int 分配给枚举。
然而,铸造将起作用:
You cannot assign an int to an enum, even if the value matches one of the enum's values.
However, casting will work:
不存在从枚举类型的值到枚举类型本身的类型转换。只能反过来。
There is no type conversion from the values of an enum type to the enum type itself. Only the other way around.