我有,我想代表作爲字符串一些錯誤代碼:如何從枚舉類型獲取std :: string?
enum class ErrorCode
{
OK,
InvalidInput,
BadAlloc,
Other
};
我想創建讓代表這些串的錯誤的直觀和簡單的方式。簡單的解決方案是:
std::string const ErrorCode2Str(ErrorCode errorCode)
{
switch (errorCode)
{
case OK:
return "OK";
case InvalidInput:
return "Invalid Input";
case BadAlloc:
return "Allocation Error";
case Other:
return "Other Error";
default:
throw Something;
}
}
有沒有更好的方法?我可以超載一個ErrorCode
串轉換嗎?我可以創建一個ErrorCode::str()
函數嗎?有沒有解決這個問題的標準解決方案?
它可以用宏來破解它:http://stackoverflow.com/questions/201593/is-there-a-simple-script-to-convert-c-enum-to-string#201792 – Adam