lundi 21 janvier 2019

Grouping constants within a class c++

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?

Aucun commentaire:

Enregistrer un commentaire