简体   繁体   中英

Enum inside enum on C++

I am trying to get working a structure based on constants. I have ObjectTypes (KEYBOARD, MOUSE, HAPTIC...) which can have InputTypes (the MOUSE can be DEFAULT and INVERSE, while KEYBOARD can be DEFAULT and NUMERIC and HAPTIC only can be DEFAULT).

To get this working I'm trying to use enums inside enums in a class called Constants in C++. It might work passing a ObjectTypes parameter and a InputTypes parameter to a function, so I need something like this in the prototype:

changeInputSystem(SimulatorConstants::InputObject input, SimulatorConstants::InputTypes type)

But in C++, when I declare an enum every value taken out from this (internally) and some of them replace the others.

My code right now (and not working) is:

    enum InputObject {
        KEYBOARD,
    MOUSE,
    HAPTIC
};

enum InputTypes {
    enum KeyboardTypes {
        DEFAULT
    };
    enum MouseTypes {
        DEFAULT,
        INVERSE
    };
    enum HapticTypes {
        DEFAULT
    };
};

NOTE: I know there is no inheritance between enums, so I need any solution that can work in a similar way. Thanks.

It works if you do the whole thing object-oriented. So instead of having enums for everything just have classes:

class InputObject {};

class Mouse : public InputObject
{
  void setDefault();
  void setInverse();
};

This way the type of the InputObject can be saved inside the object and you only pass around the actual InputObject.

Like written in the comments, if you have access to a C++11 compiler, better to use an enum class, which would change your own accepted answer (at the time of this writing) code into:

enum class KeyboardTypes {
   DEFAULT
};
enum class MouseTypes {
  DEFAULT,
   INVERSE
};
enum class HapticTypes {
   DEFAULT
};

And of course, your overloaded functions can be changed into a more natural version:

void changeInputSystem(SimulatorConstants::MouseTypes type);
void changeInputSystem(SimulatorConstants::KeyboardTypes type);
void changeInputSystem(SimulatorConstants::HapticTypes type);

And, as a bonus, there will be no implicit cast from your enums into integer values.

Ok, making some proofs I have finally solved it using overload. I've removed the ObjectTypes by overloading the method and creating three different structs:

struct KeyboardTypes {
   enum types{
      DEFAULT
   };
};
struct MouseTypes {
   enum types{
      DEFAULT,
      INVERSE
   };
};
struct HapticTypes {
   enum types{
      DEFAULT
   };
};

And the overloaded member function:

void changeInputSystem(SimulatorConstants::MouseTypes::types type);
void changeInputSystem(SimulatorConstants::KeyboardTypes::types type);
void changeInputSystem(SimulatorConstants::HapticTypes::types type);

Thanks to everyone who has helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM