fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3. typedef union multytype mt;
  4. enum types
  5. {
  6. NONE,
  7. INT,
  8. DOUBLE,
  9. CHAR,
  10. STRING,
  11. CONSTSTRING
  12. };
  13.  
  14. union multytype
  15. {
  16. types type;
  17. struct { types type; int val; } ival;
  18. struct { types type; double val; } dval;
  19. struct { types type; char val; } cval;
  20. struct { types type; char *val; } sval;
  21. struct { types type; const char *val; } csval;
  22. multytype()
  23. {
  24. type=NONE;
  25. }
  26. multytype(int val)
  27. {
  28. type=INT;
  29. ival.val=val;
  30. }
  31. multytype(double val)
  32. {
  33. type=DOUBLE;
  34. dval.val=val;
  35. }
  36. multytype(char val)
  37. {
  38. type=CHAR;
  39. cval.val=val;
  40. }
  41. multytype(char* val)
  42. {
  43. type=STRING;
  44. sval.val=val;
  45. }
  46. multytype(const char* val)
  47. {
  48. type=CONSTSTRING;
  49. csval.val=val;
  50. }
  51. };
  52.  
  53. void print(int n, ... )
  54. {
  55. mt* i=(mt*)((&n)+1);
  56. for(int j=0;j<n;j++,i++)
  57. {
  58. switch(i->type)
  59. {
  60. case INT:
  61. cout<< i->ival.val <<endl;
  62. break;
  63. case DOUBLE:
  64. cout<< i->dval.val <<endl;
  65. break;
  66. case CHAR:
  67. cout<< i->cval.val <<endl;
  68. break;
  69. case STRING:
  70. cout<< i->sval.val <<endl;
  71. break;
  72. case CONSTSTRING:
  73. cout<< i->csval.val <<endl;
  74. break;
  75. }
  76. }
  77. }
  78.  
  79. int main()
  80. {
  81. char str[]="Hi";
  82. print(5,mt(2),mt(4.2),mt('a'),mt("Hello"),mt(str));
  83. }
Runtime error #stdin #stdout 0.02s 2720KB
stdin
Standard input is empty
stdout
Standard output is empty