不限定作用域的枚举类型
注意:枚举类型为常量不可再进行赋值,也就是说需要在初始的时候对其赋值
当然也可以在初始话的时候对其赋值,不使用默认的赋值方式;
如:
enum weekday {sunday=7,monday=1,tuesday,wednesday,thursday,friday,saturday };
枚举类型可以给整型赋值,因为枚举类型是整型的子集,但是整型若想给枚举类型赋值需要进行类型转换。
代码实例:
#include <iostream> using namespace std; int main() { //定义枚举类型 enum weekday {sunday,monday,tuesday,wednesday,thursday,friday,saturday }; // 声明枚举变量 weekday tomorrow; //声明并赋值 enum weekday goalDay = monday; cout <<"直接输出goalDay的值为:" << goalDay <<"\n"; //将int 赋值给枚举 tomorrow = weekday(1); //将枚举类型赋值给整型 int daynum = monday; if (tomorrow == goalDay) cout << "tomorrow = goalday,and the day num ="<< daynum; return 0; }
运行结果;
直接输出goalDay的值为:1
tomorrow = goalday,and the day num =1