关于switch的参数类型
switch(expr1)中,expression 必须是一个整型或枚举类型,或者是一个 class 类型,其中 class 有一个单一的转换函数将其转换为整型或枚举类型。
由于,byte,short,char都可以隐含转换为int,所以,这些类型以及这些类型的包装类型也是可以的。
因此传递给 switch 和case 语句的参数应该是 int、 short、 char 或者 byte,还有enum。
string,Long、double、float都不能作用于swtich。
C++ switch 语句
http://www.runoob.com/cplusplus/cpp-switch.html
参考资料:
https://blog.youkuaiyun.com/guanghuilove/article/details/6753110
本文详细解析了switch语句的参数类型限制,指出switch(expr1)中的expression必须是整型、枚举类型或可转换为这两种类型的class。文章强调,byte、short、char及其包装类型均可用于switch语句,而string、Long、double、float则不适用。
15万+

被折叠的 条评论
为什么被折叠?



