fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. /*************************************/
  5. union Union
  6. {
  7. int i;
  8. double d;
  9. char c;
  10. };
  11. /*************************************/
  12. struct Struct
  13. {
  14. Union *wskU;
  15.  
  16. Union uObj;
  17. Union uObj2;
  18.  
  19. friend void wpisz( Struct, Union&, int );
  20. friend void wpisz( Struct, Union&, double );
  21. friend void wpisz( Struct, Union&, char );
  22.  
  23. };
  24. /*************************************/
  25. void wpisz( Struct s, Union &u, int n )
  26. {
  27. s.wskU = &u;
  28. s.wskU -> i = n;
  29. }
  30. void wpisz( Struct s, Union &u, double n )
  31. {
  32. s.wskU = &u;
  33. s.wskU -> d = n;
  34. }
  35. void wpisz( Struct s, Union &u, char n )
  36. {
  37. s.wskU = &u;
  38. s.wskU -> c = n;
  39. }
  40. /*************************************/
  41. int main( void )
  42. {
  43. Struct s;
  44. wpisz( s, s.uObj, 'X' );
  45. wpisz( s, s.uObj2, 666 );
  46.  
  47. cout << "Znak w pierwszym: " << s.uObj.c << endl;
  48. cout << "Liczba w drugim: " << s.uObj2.i << endl;
  49. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
Znak w pierwszym: X
Liczba w drugim: 666