views:

94

answers:

1

Hi all!

I wonder, whether it is possible to add/append another item to an existing enum type (part of a framework)?

Something like this: We have the enum type

  typedef enum {  
    UIModalTransitionStyleCoverVertical = 0,  
    UIModalTransitionStyleFlipHorizontal,
    UIModalTransitionStyleCrossDissolve,
    UIModalTransitionStylePartialCurl,
 } UIModalTransitionStyle;  

Now I want to append or add to this set an item like UIModalTransitionStyleCoverVerticalFlipped. Can something like this be accomplished ?

Thanks.

evangelion2100

+2  A: 

To do it, you have to modify the original type definition to include the new value:

typedef enum {  
    UIModalTransitionStyleCoverVertical = 0,  
    UIModalTransitionStyleFlipHorizontal,
    UIModalTransitionStyleCrossDissolve,
    UIModalTransitionStylePartialCurl,
    UIModalTransitionStyleCoverVerticalFlipped
} UIModalTransitionStyle;

Otherwise, you can take a chance on its not working, and define it separately:

typedef enum {  
    UIModalTransitionStyleCoverVertical = 0,  
    UIModalTransitionStyleFlipHorizontal,
    UIModalTransitionStyleCrossDissolve,
    UIModalTransitionStylePartialCurl,
} UIModalTransitionStyle;

typedef enum { 
    UIModalTransitionStyleCoverVerticalFlipped =
        UIModalTransitionStylePartialCurl + 1
} ExtendedUIModalTransitionStyle;

A variable that could hold the original enumeration will usually also work perfectly fine when/if you assign the new value as well (in a typical case, it'll just be an int) -- but it's not guaranteed. At least in theory, the implementation can/could assign few enough bits to hold that enumeration that it adding more values this way wouldn't work. It could also do range checking so assigning any out of range value wouldn't be allowed. Neither of these is at all common, so from a practical viewpoint it's probably not a problem -- but from a theoretical viewpoint, nothing really guarantees that code like this will work.

Jerry Coffin
Thanks for the two possible approaches. I will go with the second one.