#枚举注意点#
用枚举循环输出五种颜色
#include<iostream>
#include<stdio.h>
using namespace std;
enum color { red, yellow, blue, white, black };
int main()
{
string a[5] = { "red","yellow","blue","white","black" };
color i;
i = red;
color* p = &i;
for (*p = red; *p <= black; *p = color(*p + 1))//注意这里的*p = color(*p + 1)
{
cout << a[*p] << endl;
}
return 0;
}
其实上面的*p=color(*p+1)起的就是一个指针后移的作用,相当于平常写的*p++;不过这里不能这样写;
*p+1后,指针后移,同时它变成了整型,也就是说,*p+1一个整型是无法赋值给原本是color型的*p。如果实在要赋值只能强制转化。