c++强类型枚举enum class NEWTYPE

c++11以前:

include

using namespace std;

class Cat
{
public:
Cat() :m_tp(Cat::AA){}
enum MYTYPE {AA,BB,CC};
static const int DD = 1;
private:
MYTYPE m_tp;
};

int main()
{
Cat cat;

int it = Cat::DD;  //可以使用类的静态成员;
int it2 = Cat::AA;  //也可以使用类里的枚举,有点分不清;
Cat::MYTYPE it3=Cat::AA;  //ok
cout << "hello world" << endl;
system("pause");
return 0;

}

“`
c++11之后:
class Cat
{
public:
Cat() :m_tp(Cat::NEWTYPE::DD){}
enum class NEWTYPE{DD,EE,FF}; //可以与static const int同名,一般枚举不行;
static const int DD = 1;
private:
NEWTYPE m_tp;
};

int main()
{
Cat cat;
int it = Cat::DD; //ok;
int it2 = Cat::AA; //fail;
Cat::NEWTYPE it3 = Cat::NEWTYPE::DD;//ok

cout << "hello world" << endl;
system("pause");
return 0;

}

你可能感兴趣的:(c++)