开发者

Return number of Enum Values. ( Size of Enum typedef )

开发者 https://www.devze.com 2023-01-11 03:33 出处:网络
Is there a built in function or a way to query the size of an emun typedef? typedef开发者_如何学运维 enum difficultyTypes {

Is there a built in function or a way to query the size of an emun typedef?

typedef开发者_如何学运维 enum difficultyTypes {
kEasy,
kMedium,
kHard
} difficultyType;

I would like a way to query and have it ( in this case ) return 3. I could even deal with it returning 2 as the highest value ( 0,1,2 ).

Or am I forced to use another int variable that I statically set when I create the enum?


You may want to reference this post.

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

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

kEasy would be 0, kMedium is 1, kHard is 2, and kCount is 3, which is the number of elements you have minus itself.

0

精彩评论

暂无评论...
验证码 换一张
取 消