Enum Customization: is it possible to create subEnum?

So this is the code:

typedef enum{ zero, one, two, three, four, five, six, seven, eight, nine }Digits; typedef enum{ zero, one, two, nine }DigitsThatILikeToUse; 

Question: If I define a function:

 void takeMyFavoriteDigits(DigitsThatILikeToUse favorite); (C) -|+(void) takeMyFavoriteDigits:(DigitsThatILikeToUse)favorite; (Objective-C) 

I cannot use it with backreference for the base enum Digits , because my order in the enum DigitsThatILikeToUse is different.

My solution is to write the explicit position of the numbers as follows:

 typedef enum{ zero = 0, one = 1, two = 2, nine = 9 }DigitsThatILikeToUseInEdition; 

But! I cannot repeat this new enum DigitsThatILikeToUseInEdition .

I want to create a subEnum in enum and repeat it. is it possible?

My best idea is to use something like this:

 typedef enum{ beginIteratorDigitsThatILike, zero, one, two, nine, endIteratorDigitsThatILike, three, four, five, six, seven, eight }Digits; 

But maybe some solutions?

+4
source share
1 answer

As (basically) there is no problem that cannot be solved by adding additional levels of indirection:

 typedef enum enumDigits { digitsNone = -1, digitsZero, digitsOne, digitsTwo, digitsThree, digitsFour, digitsFive, digitsSix, digitsSeven, digitsEight, digitsNine, digitsMax } Digits_t; typedef enum enumDigitsIndexIdLikeToUse { digitsIndexIdLikeToUseNone = -1, digitsIndexIdLikeToUseZero, digitsIndexIdLikeToUseOne, digitsIndexIdLikeToUseTwo, digitsIndexIdLikeToUseThree, digitsIndexIdLikeToUseFour, digitsIndexIdLikeToUseMax } DigitsIndexIdLikeToUse_t; const Digits_t digitsIdLikeToUse[digitsIndexIdLikeToUseMax] = { digitsZero, digitsOne, digitsTwo, digitsNine } 

Assuming you want to use at least 1 didigt, you can do:

 Digits_t digitIdLikeToUse = digitNone; ... for (DigitsIndexIdLikeToUse_t digitIndexIdLikeToUse = digitsIndexIdLikeToUseZero, digit = digitsIdLikeToUse[digitIndexIdLikeToUse]; digitsIndexIdLikeToUse < digitsIndexIdLikeToUseMax; ++ digitsIndexIdLikeToUse) { <do something with digitIdLikeToUse> } 
+1
source

Source: https://habr.com/ru/post/1444337/


All Articles