2011-05-06 220 views
4

我试图使用具有声明的类里,像这样的枚举类型的类内:枚举C++类

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#并且通常看到声明为一个类的OUums的枚举,而不是INSIDE就像它在这里。我如何初始化枚举类型。例如,我想要做这样的事情:

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

显然这是行不通的。我将如何做到这一点?

回答

9

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

class x{ 
//Your other stuff 

XEnumType myEnum; 
}; 

int main(void) 
{ 
    x myNewClass(); 
    x.myEnum = x::LINEAR; 
} 
+0

+1,打我吧。此外,你应该摆脱前面enum的typedef关键字(只需使用'enum XEnumType {...}') - 你现在在C++中。 – Tim 2011-05-06 15:14:59

+0

不伤害有它,但你是对的,typedef是不必要的在c + + – 2011-05-06 15:15:26

6

第一:不要t使用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; 
} 
+1

“不要使用typedef”。你能解释为什么不呢? – 2013-03-23 21:07:36

+3

@dpk:在C语言中,声明一个'struct MyStruct {};'需要你始终引用类型为'struct MyStruct'。这就是为什么人们做了'typedef struct {} MyStruct;',所以他们只能使用'MyStruct'作为类型。 C++没有这个要求;你可以声明'struct MyStruct {};'并且仍然使用'MyStruct'作为类型。在这种情况下应用'typedef'是多余的。 – DevSolar 2014-05-06 08:13:31

2

你宣布一个新的类型: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; 
0

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

定义XEnumType,其实这是多余的呢 - 更喜欢类似于:

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) {} 
4
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; 
0

让我先假设一些前提条件:

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

您的问题是这些常数值在类x中声明。所以初始化的x你需要指定范围的一个实例:

而不是

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

尝试

x myX(x::LINEAR); 

通过指定x::您提供恒定的范围。