Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 18th, 2012  |  syntax: None  |  size: 1.05 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Accessing the values of an enum defined in a struct
  2. struct padData
  3. {
  4.     enum buttonsAndAxes
  5.     {
  6.         select,
  7.         start,
  8.         ps
  9.     };
  10. };
  11.        
  12. padData        pad;
  13.        
  14. printf ("n%d", pad.buttonsAndAxes[0]);
  15.        
  16. error: invalid use of ‘enum padData::buttonsAndAxes’
  17.        
  18. printf ("n%d", pad::buttonsAndAxes[0]);
  19.        
  20. error: ‘pad’ is not a class or namespace
  21.        
  22. printf ("nemit: %d", padData::(select)0);
  23.        
  24. error: expected unqualified-id before ‘(’ token
  25.        
  26. enum buttonsAndAxes
  27. {
  28.     select,
  29.     start,
  30.     ps
  31. };
  32.  
  33. const char* buttonsAndAxesNames[] = {
  34.     "select",
  35.     "start",
  36.     "ps"
  37. };
  38.        
  39. printf("%s", buttonsAndAxesNames[select]);
  40.        
  41. enum <enum-name> {
  42.   <value-name-0> [= <value-0>],
  43.   <value-name-1> [= <value-1>],
  44.   ...
  45. };
  46.        
  47. enum <enum-name> {
  48.   <value-name-0> [= <value-0>],
  49.   <value-name-1> [= <value-1>],
  50.   ...
  51. };
  52.        
  53. enum Color {
  54.   Blue,
  55.   Green,
  56.   Red
  57. };
  58.  
  59. char const* name(Color c) {
  60.   switch(c) {
  61.   case Blue: return "Blue";
  62.   case Green: return "Green";
  63.   case Red: return "Red";
  64.   }
  65.   assert(0 && "Who stored crap in my enum ?");
  66. }