2011-05-06 188 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::您提供恆定的範圍。