0

I'm attempting to write out error codes that are stored publicly within a class. The are simple integers. My problem is that I don't want a bunch of const ints floating around the class; I'd like them grouped somehow.

namespaces don't seem like an option, and I'm not sure class is the way to go. What I'm looking for:

class Foo {
    someContainer Errors {
        int const x = 1;
        int const y = 1;
        // ...
    }
}

I've tried enums so far, but I would like to use ints instead of enums if possible (I'd like to return ints and allow implementing applications using the class to compare ints instead of enums).

class Foo {
    enum class Errors {
        no_error = 0,
        x = 1001,
        y = 1002
    }
}

Thoughts on what is best practice for something like this?

Frank
  • 915
  • 1
  • 7
  • 22
  • `I'd like to return ints and allow implementing applications using the class to compare ints instead of enums` Why? `if(do_something() == FILE_NOT_FOUND)` is way cleaner than `if(do_something() == 15463)`. – tkausl Jan 21 '19 at 17:06
  • @tkausl That’s a good point. Mostly I was worried about optimization returning enums from every function call... I agree with you though. My worry may be moot – Frank Jan 21 '19 at 17:29
  • `Mostly I was worried about optimization returning enums from every function call...` What kind of optimization specifically? Enums _are_ just integers. – tkausl Jan 21 '19 at 18:00
  • Generally we all use enums for this - they are very convenient. Of course, in the wider sense your constants might not be integers. They could be well-formed reals, for example. The latest C++ standards allow declarations of `static const int name {value};` (or other simple types) without needing to instance the static, though sometimes enabling debug symbols requires the statics are explicitly instanced. – Gem Taylor Jan 21 '19 at 18:24
  • Note that the referenced duplicate predates general c++11 acceptance – Gem Taylor Jan 21 '19 at 18:26

0 Answers0