fork download
  1. #include <iostream>
  2. using namespace std;
  3. class Foo
  4. {
  5. public:
  6. Foo(){cout<<"Foo"<<endl;}
  7. ~Foo(){cout<<"~Foo"<<endl;}
  8. int f;
  9. };
  10. class Bar
  11. {
  12. public:
  13. Bar(){cout<<"Bar"<<endl;}
  14. ~Bar(){cout<<"~Bar"<<endl;}
  15. int b;
  16. };
  17.  
  18. union U
  19. {
  20. U(){}
  21. ~U(){}
  22. Foo f;
  23. int a;
  24. Bar b;
  25. };
  26. int main() {
  27. U uu;
  28. cout<<"=========== 1" <<endl;
  29. uu.f.f = 123;
  30. cout<<"=========== 2" <<endl;
  31. uu.a = 123;
  32. cout<<"=========== 3" <<endl;
  33. uu.b.b = 123;
  34. cout<<"=========== 4" <<endl;
  35. // your code goes here
  36. return 0;
  37. }
Success #stdin #stdout 0s 4452KB
stdin
Standard input is empty
stdout
=========== 1
=========== 2
=========== 3
=========== 4