工程上很多情况都需要用到错误码,便于更好的定位异常问题。
下面把ErrorCode写在类中的实现,最后打印的结果是state =11,正确;
#include <iostream>
#include <thread>
#include <math.h>
#include <vector>
#include <queue>
class ErrorCode{
public:
enum state{
Success = 10,
Failure1,
Failure2,
Failure3
};
};
int main()
{
ErrorCode ec;
ErrorCode::state s = ErrorCode::state::Failure1;
std::cout << "state =" << s << std::endl;
}
感觉写成class比较复杂,声明时要写好几层::,所以把ErrorCode写成结构体的形式,并用函数result()调用判断后返回ErrorCode。用0和10尝试结果,可行。
#include <iostream>
#include <thread>
#include <math.h>
#include <vector>
#include <queue>
enum ErrorCode{
Success = 10,
Failure1,
Failure2,
Failure3
};
ErrorCode result(int i){
if(0 == i) return ErrorCode::Success;
if(0 != i) {return Failure3;}
else return Failure2;
}
int main()
{
ErrorCode s1 = result(0);
std::cout << "state1 =" << s1 << std::endl;
ErrorCode s2 = result(10);
std::cout << "state2 =" << s2 << std::endl;
}