简体   繁体   English

枚举在C ++上的enum里面

[英]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). 我有ObjectTypes (KEYBOARD,MOUSE,HAPTIC ...),它可以有InputTypes (MOUSE可以是DEFAULT和INVERSE,而KEYBOARD可以是DEFAULT,NUMERIC和HAPTIC只能是DEFAULT)。

To get this working I'm trying to use enums inside enums in a class called Constants in C++. 为了实现这一点,我正在尝试在C ++中名为Constants的类中使用枚举内部的枚举。 It might work passing a ObjectTypes parameter and a InputTypes parameter to a function, so I need something like this in the prototype: 它可能会将ObjectTypes参数和InputTypes参数传递给函数,所以我需要在原型中使用这样的东西:

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. 但是在C ++中,当我声明一个枚举时,从这个(内部)取出的每一个值都会取代其他值。

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. 这样,InputObject的类型可以保存在对象中,并且只传递实际的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: 就像在评论中写的那样,如果你有权访问C ++ 11编译器,最好使用枚举类,这会将你自己接受的答案(在撰写本文时)改为:

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: 我通过重载方法并创建三个不同的结构来删除ObjectTypes:

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. 感谢所有帮助过的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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