将变量的可取值全部列举出来,写在程序的开头,使用该类型的时候显示地指定取值即可(即对枚举变量进行赋予元素操作,这里之所以叫赋元素操作不叫赋值操作就是为了说明枚举变量是不能直接赋予算数值的)。

如:

enum egg {a,b,c}; 

enum egg test;     //在这里可以简写egg test; 

test = c;

 

注:

1)枚举类型具有默认值。

2)枚举类型可以进行关系运算。

3)要将整数值赋值给枚举类型时,要进行强制类型转换。

 

Code:

#include<iostream>

#include<stdio.h>

using namespace std;

enum egg {a=1,b,c};

int main()

{

    enum egg test;

    printf("%d\n",test);

 

    test = c;

    printf("%d\n",test);

 

    egg test1=egg(a);

    printf("%d",test1);

}

输出结果:2 3 1 

 

by TZ 2017.3.25

相关文章:

  • 2022-01-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-18
  • 2022-12-23
  • 2021-06-18
  • 2022-12-23
  • 2022-01-10
  • 2021-04-06
  • 2021-09-29
相关资源
相似解决方案