0

I have my error codes (about 30 error codes) stored in an enum. A collegue warned me about it because if I change the numerical value of an enum, all code which uses my DLL will have to recompile or else the error codes become inconsistent. I understand the problem: enums become integer constants during the compile process, so the DLL uses the enum values which were valid at compile time, not at runtime as I would prefer.

My question is how I can do better. Please help me. In the following, I explain an idea of mine, but better ideas are welcome if my idea has severe disadvantages.

My first idea is to make a class and store the codes as static constant members

class ErrorCodes
{
public:
  static const int ok;
  static const int error1;
  static const int error2;
};

and define their values in the .cpp

const int ErrorCodes:ok = 0;
const int ErrorCodes:error1 = -1;
const int ErrorCodes:error2 = -2;

Does this fix the above problem? What happens if I add or remove members? What happens if I change the values of the error codes?

Fabian
  • 4,001
  • 4
  • 28
  • 59
  • 1
    It's simple: Don't change the order or values of the enumerations. Just add new to the end if you need. And if you *must* change the order, or remove some, then document it well as a breaking version change, needing not only a recompile but also as breaking backward compatibility (which is the real problem here). Recompilation is needed if new error codes are added no matter what scheme you use. – Some programmer dude Feb 25 '16 at 07:03
  • Your solution seems fine to me (might give a slight performance penalty), but why would you want to change the numerical values of your enums in the first place. – MikeMB Feb 25 '16 at 07:08
  • 3
    Wouldn't it be annoying if you went to the bank and found that instead of having a balance you now had debt because the bank had renumbered your account but your checkbook still had the old number. Lesson: Don't renumber things unless you really, really need to. If you need to remove an enum, just deprecate it... `error2` => `depr_error2`, so that the order of the remainder is consistent. – kfsone Feb 25 '16 at 07:15
  • You can also give enumerations fixed values and you never change them. It will work the same as your int solution. – rozina Feb 25 '16 at 07:25

0 Answers0