关于enum的使用
关于枚举enum的使用,示例带码如下:
//:enum:enum.cpp
#include <iostream>
#include <cassert>
using namespace std;
enum Hue{red,blue,yellow};//red初始化为0,blue为1,yellow为2
const char* printHue(Hue h)
{
static const char* hues[]={"red","blue","yellow"};//指针数组,每个元素指向static const char类型的数据
assert(red<=h&&h<=yellow);
return hues[h];
}
class color
{
private:Hue hue;
public:
color(Hue h):hue(h){}
void setHue(Hue h)
{
hue=h;
}
Hue getHue()
{
return hue;
}
};
int main()
{
color c(red);
cout<<c.getHue()<<endl;
cout<<printHue(c.getHue())<<endl;
c.setHue(yellow);
cout<<printHue(c.getHue())<<endl;
system("pause");
}
///:~