enum class Enumeration{ Val1, Val2, Val3 = 100, Val4 /* = 101 */,};
此种枚举为类型安全的。枚举类型不能隐式地转换为整数;也无法与整数数值做比较。
#include <iostream>
using namespace std ;
int main(int argc, char **argv)
{
enum class Status {RUN,ERROR,FINISHED,START,STOP};
Status proStatus1 {Status::START};
Status ProStatus2{Status::ERROR};
Status nowStatus;
nowStatus=Status::STOP;
for (int i=0;i<2;i++){
if (nowStatus==Status::STOP){
nowStatus=Status::START;
}
else if(nowStatus==Status::START){
nowStatus=Status::RUN;
}
}
cout<<(long)nowStatus;
return 0;
}
输出 0
#include <iostream>
using namespace std ;
int main(int argc, char **argv)
{
enum class Status {RUN=10,ERROR,FINISHED,START,STOP};
Status proStatus1 {Status::START};
Status ProStatus2{Status::ERROR};
Status nowStatus;
nowStatus=Status::STOP;
for (int i=0;i<3;i++){
if (nowStatus==Status::STOP){
nowStatus=Status::START;
}
else if(nowStatus==Status::START){
nowStatus=Status::RUN;
}
else if(nowStatus==Status::RUN){
nowStatus=Status::FINISHED;
}
}
cout<<(long)nowStatus;
return 0;
}
输出12
#include <iostream>
using namespace std ;
int main(int argc, char **argv)
{
enum class Status:long {RUN=10,ERROR,FINISHED,START,STOP};
Status proStatus1 {Status::START};
Status ProStatus2{Status::ERROR};
Status nowStatus;
nowStatus=Status::STOP;
for (int i=0;i<3;i++){
if (nowStatus==Status::STOP){
nowStatus=Status::START;
}
else if(nowStatus==Status::START){
nowStatus=Status::RUN;
}
else if(nowStatus==Status::RUN){
nowStatus=Status::FINISHED;
}
}
cout<<(long)nowStatus<<endl;
cout<<static_cast<long>(nowStatus)<<endl;
return 0;
}
输出12
来源:CSDN
作者:AI_LX
链接:https://blog.csdn.net/AI_LX/article/details/104135749