c, obj c enum without tag or identifier

后端 未结 3 1523
太阳男子
太阳男子 2021-01-17 06:09

im learning cocos2d [open gl wrapper for objective C on iPhone], and now playing with sprites have found this in a example,

 enum {  
easySprite =   0x000000         


        
相关标签:
3条回答
  • 2021-01-17 06:43

    Well, you seem to be working off a terrible example. :)

    At least as far as enums are concerned. It's up to anyone to define the actual value of an enum entry, but there's no gain to use hex numbers and in particular there's no point in starting the hex numbers with a through f (10 to 15). The example will also work with this enum:

    enum {  
    easySprite = 10,
    mediumSprite,
    hardSprite,
    backButton,
    magneticSprite,
    magneticSprite2
    };
    

    And unless there's some point in having the enumeration start with value 10, it will probably work without specifying any concrete values.

    0 讨论(0)
  • 2021-01-17 06:48

    Enums are automatically assigned values, incremented from 0 but you can assign your own values.

    If you don't specify any values they will be starting from 0 as in:

    typedef enum {
     JPG,
     PNG,
     GIF,
     PVR
     } kImageType;
    

    But you could assign them values:

    typedef enum {
     JPG = 0,
     PNG = 1,
     GIF = 2,
     PVR = 3
     } kImageType;
    

    or even

    typedef enum {
     JPG = 100,
     PNG = 0x01,
     GIF = 100,
     PVR = 0xff
     } kImageType;
    

    anything you want, repeating values are ok as well.

    I'm not sure why they are given those specific values but they might have some meaning related to use.

    0 讨论(0)
  • 2021-01-17 06:54

    Usually, when you are creating an enum, you want to use it as a type (variable, method parameters etc.).

    In this case, it's just a way how to declare integer constants. Since thay don't want to use the enum as type, the name is not necessary.

    Edit: Hexadecimal numbers are commonly used when the integer is a binary mask. You won't see any operators like +,-,*,/ used with such a number, you'll see bitwise operators (!, &, |, ^).

    Every digit in a hexadecimal number represents 4 bits. The whole number is a 32-bit integer and by writing it in hexadecimal in this case, you are saying that you are using only the last four bits and the other bits can be used for something else. This wouldn't be obvious from a decimal number.

    0 讨论(0)
提交回复
热议问题