We help IT Professionals succeed at work.

multiple enums in single class

ambuli
ambuli asked
on
Hi Experts,

I have a class that should have two different enums.

class Controller
{
   enum { CmdOne, CmdTwo, CmdThree, etc };
   enum { ValueOne, ValueTwo, ValueThree, etc };

}
When I use them, I want them to be accessed differently( just pointing out they are from different groups)
for example, now, there is no difference between  Controller::CmdOne and Controller::ValueOne,
Comment
Watch Question

Senior Software Engineer (Avast)
CERTIFIED EXPERT
Commented:
Embed the enum in a struct, the struct then becomes a namespace for the enum. Below is an example. Since you didn't say how each enum should differ I've just broken up your example to show you how it's done.
class Controller
{
   // encapsulate enums in a struct
   struct Cmd {
      enum Type { One, Two, Three };
   };

   struct Value {
      enum Type { One, Two, Three };
   };

}

// Now you can access them like this
Controller::Cmd::Type c1 = Controller::Cmd::One;
Controller::Cmd::Type c2 = Controller::Cmd::Two;
Controller::Cmd::Type c3 = Controller::Cmd::Three;


Controller::Value::Type v1 = Controller::Value::One;
Controller::Value::Type v2 = Controller::Value::Two;
Controller::Value::Type v3 = Controller::Value::Three;

Open in new window

CERTIFIED EXPERT

Commented:
Try this:
class Controller
{
enum X { CmdOne, CmdTwo, CmdThree, etc };
enum Y { ValueOne, ValueTwo, ValueThree, etc };
};

Controller::X::CmdOne
Controller::Y::ValueOne

Author

Commented:
Thank you.  Is it a "normal" way of doing it?
Bill NolanOwner, Lead Technology Programmer

Commented:
Yes; it is more typical to name them as described than not, and better practice in most cases.  That is because naming them also clarifies what they are.

Author

Commented:
Thank you both.  I also see some references to using namespace to achieve this.  However, the enum is declared outside the class.  Which one would be better?


namespace Cmd
{
   enum { One, Two, Three };
}

namespace Value
{
   enum {  One, Two, Three  };
}
class Controller
{


};
Bill NolanOwner, Lead Technology Programmer
Commented:
It's solely a matter of where it makes the most sense.  I.e., does the enum belong in a specific class, or is it general to the entire namespace?  E.g., you might have an enum of "controller types" that would certainly belong within the controller class.  Also, even in the examples above where your enums aren't specific to a "controller", it would still usually be better to give the enums a name.  E.g.:

namespace Value
{
   enum Numbers {  One, Two, Three  };
}
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
ambuli,

You've accepted http:#a37086873 but that won't actually work in standards compliant C++ since the enum's name does not provide a namespace for the enum values.


>>Thank you.  Is it a "normal" way of doing it?

If you are referring to my answer, yes it is a common C++ idiom to encapsulate an enum in a struct. You can, as Slimfinger observes, also use a namespace but that doesn't work so well if you are doing generic programming (you can't pass a namespace as a type to a template). It is; therefore, more common to use a struct to give your enum a named scope.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.