fork download
  1. #include <iostream>
  2.  
  3. struct Base {
  4. enum Type {
  5. FOO = 0,
  6. BAR = 1
  7. };
  8. virtual ~Base() {}
  9. virtual Type type() const = 0;
  10. int value_;
  11. };
  12.  
  13. struct Foo : Base {
  14. Foo() { value_ = 33; }
  15. virtual Type type() const { return FOO; }
  16. };
  17.  
  18. struct Bar : Base {
  19. Bar() { value_ = 44; }
  20. virtual Type type() const { return BAR; }
  21. };
  22.  
  23. int main() {
  24. Foo foo;
  25. Bar bar;
  26. Base & b = foo;
  27. std::cout << b.type() << ", " << b.value_ << "\n";
  28. b = bar;
  29. std::cout << b.type() << ", " << b.value_ << "\n";
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0.02s 2680KB
stdin
Standard input is empty
stdout
0, 33
0, 44