I have some class with Load() function, for example.
class DB {
private:
pt_db *db;
public:
DB(const char *path);
Write(const char *path);
int Load(const char *path);
};
And I want to return some status from Load() function depending on the passed argument.
For example:
Load(<correct path to the file with valid content>) // return 0 - success
Load(<non-existent path to file>) // return 1
Load(<correct file path, but the content of the file is wrong>) // return 2
Nevertheless also I'm worrying about:
Type safety - I mean I want to return some object which could only be used as status code.
int res = Load(<file path>); int other = res * 2; // Should not be possibleUse only predefined values. With
intI can return, by error, some other status likereturn 3(let's suggest something wrong has happened inLoad()function) and if I don't expect this error code will be passed:int res = Load(<file path>); if(res == 1) {} else if (res == 2) {}; ... // Here I have that code fails by reason that Load() returned non-expected 3 valueUse best C++11 practises about it.
Could anyone help?
enum classdo this?error_codeis used by the standard library to report system errors in a standardised way, by throwing asystem_errorwhich contains anerror_code. Also, you can't use the code directly from C since C can't handle classes either.