c++: enum inside of a class using "enum class"

10.1k views Asked by At

What would be the right way to write an enum inside a class? I am writing conway's game of life code in c++. So i have a patterns class which contains the info about different kind of patterns:

class Patterns
{
public:
    Patterns();
    ~Patterns(void);

private:
    enum class PatternType
    {
        bloat,
        block,
        blinker,
        toad
    };

    PatternType pattern_;
};

My goal is not to pollute the global space here. So is it the write way of writing the enum inside of a class keeping oops concepts in mind. If not, please suggest some better alternatives and what are their benefits over this method. Any inputs on keeping things in mind while writing enums inside of a class are welcome.

Edit: @Jay Miller Based on his inputs i have tried all of his three methods

enum class PatternClassOut{ point, circle, triangle };

class Patterns
{
public:
  enum PatternSimple{bloat, block, blinker, toad};
  enum class PatternClassIn{cat, mouse, dog};
  //get set functions for all the enum variables

private:
  PatternClassOut  out_;
  PatternSimple    simple_;
  PatternClassIn   in_;
};

My findings (After trying out the code & googling a bit):

  1. PatternSimple is the simplest way to write the enum in my case, inside of the class; but it compromises on type safety i.e i can compare the enum values with any other data type and it wont give any error.
  2. PatternClassIn provides type safety; but i have to write more code in the calling funcion, to access the enum values.
  3. PatternClassOut provides the best of both worlds. It provides type safety, moreover, the code from the calling function to access enum values is same as that of PatternSimple.
3

There are 3 answers

1
Jay Miller On BEST ANSWER

Assuming you want this to be available outside your class, making the enum public makes sense. As an enum class, you will need the enum name to access the values in addition to the class name:

class Patterns {
public:
    enum class PatternType { ... };
...
};
void makePattern(Patterns::PatternType value) { ... };
makePattern(Patterns::PatternType::bloat);

Because it is an enum class, it may be perfectly valid to move it outside of another class. The only addition to the global namespace will be the name of the enum.

enum class Patterns {
   bloat,
   ...
};
void makePattern(Patterns value) { ... };
makePattern(Patterns::bloat);

If you have other operations you want to group with the enum then nesting it in a class as you have makes sense. You may decide, in that case, to just use enum rather than enum class:

class Patterns {
public:
    enum PatternType { ... };
    std::string toString() const { ... };
    ...
private:
    PatternType value;
};
...
...
void makePattern(Patterns::PatternType value) { ... };
makePattern(Patterns::bloat);
4
Izzy On

Your approach is ok, only drawback would be if you are trying to access PatternType from outside the class it would be unreachable since it is declared private.

7
Lightness Races in Orbit On

I'd make it public, but otherwise you've done it just as I would.