fork download
  1. #include <iostream>
  2.  
  3. class Base
  4. {
  5. public:
  6. virtual void print()
  7. {
  8. std::cout << "Hello from Base ";
  9. }
  10. };
  11.  
  12. class Derived : public Base
  13. {
  14. public:
  15. virtual void print()
  16. {
  17. std::cout << "and Derived!" << std::endl;
  18. }
  19. };
  20.  
  21. int main(void);
  22. int main()
  23. {
  24. Base foo;
  25. Derived bar;
  26. union foobar {Base *b; Derived *d;} fb;
  27.  
  28. fb.b = &foo; fb.b->print();
  29. fb.d = &bar; fb.d->print();
  30.  
  31. return 0;
  32. }
Success #stdin #stdout 0s 4408KB
stdin
Standard input is empty
stdout
Hello from Base and Derived!