#define ENUM_DECLARE(class_type, enum_type) \
public: \
enum_type value; \
operator enum_type(){return value;} \
class_type(){} \
class_type(int val){ value = (enum_type)val;} \
class_type(const enum_type& n):value(n){}
用法示例:
#include <iostream>
using namespace std;
#define ENUM_DECLARE(class_type, enum_type) \
public: \
enum_type value; \
operator enum_type(){return value;} \
class_type(){} \
class_type(int val){ value = (enum_type)val;} \
class_type(const enum_type& n):value(n){}
struct Day
{
enum _Day
{
One = 1,
Two,
Three
};
ENUM_DECLARE(Day, _Day)
};
struct Night
{
enum _Night
{
One = 1,
Two,
Three,
Four
};
ENUM_DECLARE(Night, _Night)
};
void test(Night n)
{
cout << n << "in test"<<endl;
}
void test_day(Day d)
{
cout << d << "in test"<<endl;
}
int main()
{
Night night;
night = Night::One;
cout <<night<<endl;
if(night == 1)
cout <<"assert(night ==1)"<<endl;
else
cout <<night<<endl;
int a = (int)night;
cout <<"a = " <<a<<endl;
enum aa{b, cc};
aa bb;
bb=(aa)3;
night = (int)4;
night = 4; //超出的权限
//bb = 4; //error
if(night == Night::Four)
cout <<"Four"<<endl;
else
cout <<night<< "== "<<endl;
test(night);
test(Night::One);
test(Night::Two);
cout <<"xxx"<<endl;
Day day;
day = Day::One;
cout <<day<<endl;
test_day(Day::Three);
test_day(day);
char c;
cin>>c;
return 0;
}