2010-08-13 39 views
1

是否有內置函數或查詢emun typedef大小的方法?返回Enum值的數量。 (Enum typedef的大小)

typedef enum difficultyTypes { 
kEasy, 
kMedium, 
kHard 
} difficultyType; 

我想一個方法來查詢,並把它(這種情況下)返回3. 我甚至可以處理它返回2的最高值(0,1,2)。

或者我不得不使用另一個int變量,我創建枚舉時靜​​態設置?

+0

您可以將kMax添加到最後 – PostMan 2010-08-13 01:02:22

回答

4

您可能想參考this post

To clarify his answer, looking at your example you could do the following 

typedef enum difficultyTypes { 
kEasy, 
kMedium, 
kHard, 
kCount 
} difficultyType; 

kEasy將是0,kMedium是1,kHard是2,並且kCount是3,這是你必須減去自身元件的數量。