【问题标题】:Enums inside of a C++ classC++ 类中的枚举
【发布时间】:2011-05-06 15:10:34
【问题描述】:

我正在尝试使用一个在类中声明枚举类型的类,如下所示:

class x {
public:
    x(int);
    x( const x &);
    virtual ~x();
    x & operator=(const x &);
    virtual double operator()() const;

    typedef enum  {
        LINEAR = 0,      /// Perform linear interpolation on the table
        DIPARABOLIC = 1  /// Perform parabolic interpolation on the table
    } XEnumType; 
};

我需要声明这个类的一个实例并初始化枚举类型。我来自 C#,通常会看到声明在类之外的枚举,而不是像这里一样的 INSIDE。如何初始化枚举类型。例如,我想做这样的事情:

x myX(10);   
myX.XEnumType = Linear;

显然这不起作用。我该怎么做?

【问题讨论】:

    标签: c++ enums


    【解决方案1】:

    首先你需要在你的类中声明一个XEnumType类型的变量 然后您可以使用作用域的类名访问实际的枚举值:x::LINEARx::DIPARABOLIC

    class x{
     //Your other stuff
    
    XEnumType myEnum;
    };
    
    int main(void)
    {
        x myNewClass();
        x.myEnum = x::LINEAR;
    }
    

    【讨论】:

    • +1,打败我。另外,你应该去掉枚举前面的 typedef 关键字(只需使用 enum XEnumType {...})——你现在在 C++ 中。
    • 拥有它并没有什么坏处,但你是对的,c++中不需要typedef
    【解决方案2】:

    第一:不要使用typedef。相反,将枚举的名称放在其头部

    enum XEnumType {
        LINEAR = 0,      /// Perform linear interpolation on the table
        DIPARABOLIC = 1  /// Perform parabolic interpolation on the table
    };
    

    简而言之,像你那样做会大部分表现相同,但在神秘的极端情况下会有所不同。您使用的语法与我上面仅在 C 中使用的语法有很大不同。

    第二:这只是定义了一个类型。但是您想定义该枚举的对象。这样做:

    XEnumType e;
    

    总结:

    class x {
        /* ... stays the same ... */
    
        enum XEnumType {
            LINEAR = 0,      /// Perform linear interpolation on the table
            DIPARABOLIC = 1  /// Perform parabolic interpolation on the table
        }; 
    
        XEnumType e;
    };
    
    void someFunction() {
        x myX(10);
        myX.e = x::LINEAR;
    }
    

    【讨论】:

    • “不要使用 typedef。”你能解释一下为什么不吗?
    • @dpk:在 C 中,声明 struct MyStruct {}; 要求您始终将类型称为 struct MyStruct。这就是人们使用typedef struct {} MyStruct; 的原因,所以他们可以只使用MyStruct 作为类型。 C++ 没有这个要求;您可以声明 struct MyStruct {}; 并仍然使用 MyStruct 作为类型。在这种情况下应用typedef 是多余的。
    【解决方案3】:
    enum XEnumType {
        LINEAR, DIPARABOLIC
    };
    
    class x {    
        public:    
          x(int);    
          x( const x &);    
          virtual ~x();    
          x & operator=(const x &);    
          virtual double operator()() const;    
          XEnumType my_enum;
    };
    

    用法:

    x myX(10);
    myX.my_enum = LINEAR;
    

    【讨论】:

      【解决方案4】:

      您声明了一个新类型:XEnumType。您必须在 x 类中创建该类型的字段。 . 例如:

      class x {
      
      public:
          x(int);
          x( const x &);
          virtual ~x();
          x & operator=(const x &);
          virtual double operator()() const;
      typedef enum  {
          LINEAR = 0,      /// Perform linear interpolation on the table
          DIPARABOLIC = 1  /// Perform parabolic interpolation on the table
      } XEnumType; 
      public:
      XEnumType type;
      };
      

      那你就可以这样访问了:

      x foo(10);
      foo.type = LINEAR;
      

      【讨论】:

      • 嗨@Heisenbug:我认为简单地调用LINEAR 是行不通的。您需要拨打x::LINEAR
      【解决方案5】:

      线

      typedef enum  {
          LINEAR = 0,      /// Perform linear interpolation on the table
          DIPARABOLIC = 1  /// Perform parabolic interpolation on the table
      } XEnumType; 
      

      定义了一个名为XEnumTypetype,实际上这无论如何都是多余的——更喜欢这样的东西:

      enum XEnumType
      {
        LINEAR = 0,      /// Perform linear interpolation on the table
        DIPARABOLIC = 1  /// Perform parabolic interpolation on the table
      };
      

      现在你需要在你的类中定义一个这种类型的成员

      XEnumType _eType;
      

      在你的构造函数中,你可以初始化为任何东西

      x::x(int ) : _eType(x::LINEAR) {}
      

      【讨论】:

        【解决方案6】:

        让我先假设一些先决条件:

        • x 类来自第三方库,因此无法更改。
        • x 类在枚举的帮助下定义了一些整数常量。
        • x 类应该使用常量LINEARDIPARABOLIC 之一进行初始化。

        您的问题是这些常量值是在x 类中声明的。因此,要初始化x 的实例,您需要指定范围:

        代替

        x myX(10);   
        myX.XEnumType = Linear;
        

        试试

        x myX(x::LINEAR);
        

        通过指定x::,您可以提供常量的范围。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-11-26
          • 2012-02-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-26
          相关资源
          最近更新 更多