gaussblurinc
gaussblurinc

Reputation: 3692

Enum Customization: Is it possible to create subEnum?

So, this is code:

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

typedef enum{
    zero, one, two, nine 
}DigitsThatILikeToUse;

Issue: If i define function:

void takeMyFavoriteDigits(DigitsThatILikeToUse favorite); (C)

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

I can't use it with backreference to basic enum Digits because my order in enum DigitsThatILikeToUse is different.

My solution is to write explicit position of numbers like this:

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

But! i can't iterate through this new enum DigitsThatILikeToUseInEdition.

I want to create an subEnum in enum and iterate through 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 there any solutions?

Upvotes: 4

Views: 260

Answers (1)

alk
alk

Reputation: 70971

As (mostly) there is no problem that can not be solve by adding more 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 could do:

Digits_t digitIdLikeToUse = digitNone;

...

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

Upvotes: 1

Related Questions