[英]Casting enum variable to enum type
I have encountered a code where enum
variable is casted to enum
type. 我遇到了将
enum
变量强制转换为enum
类型的代码。 I am unable to understand the use of such casting. 我无法理解这种转换的用法。 For E. g.
对于E。
typedef enum{x,y,z} en;
void main(){
en const v;
switch(v){
case (en)y : {
}
break;
case (en)x: {
}
break;
default : {
foo();
}
break;
}
}
What is the use of casting x
or y
to enum
type that is (en)x
in case label? 在大小写标签中,将
x
或y
强制转换为(en)x
enum
类型有什么用?
Such code is symptomatic of sheer paranoia, that's all. 这样的代码只是纯粹的妄想症的征兆。 In fact, it's possibly harmful since in forcing a cast, you're hinting to the compiler that you know what you're doing so it may not warn you of an errant case label.
实际上,这可能是有害的,因为在强制执行强制转换时,您是在向编译器暗示您知道自己在做什么,因此它可能不会警告您错误的case标签。
switch
ing directly on an enum
is perfectly valid, as enum
s are essentially integral types. 直接在
enum
上switch
是完全有效的,因为enum
本质上是整数类型。 You can safely drop the (en)
casts. 您可以放心
(en)
强制转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.