fork download
  1. #include <iostream>
  2. struct Base
  3. {
  4. virtual void Serialize(std::ostream &os) const
  5. {
  6. os << "Base";
  7. }
  8. };
  9.  
  10. struct Derived : Base
  11. {
  12. virtual void Serialize(std::ostream &os) const override
  13. {
  14. os << "Derived, ";
  15. Base::Serialize(os);
  16. }
  17. };
  18.  
  19. //...
  20.  
  21. std::ostream &operator<<(std::ostream &os, Base const &b)
  22. {
  23. b.Serialize(os);
  24. return os;
  25. }
  26.  
  27. //...
  28.  
  29. int main()
  30. {
  31. Base b;
  32. Derived d;
  33. Base &bd = d;
  34. std::cout << b << std::endl;
  35. std::cout << d << std::endl;
  36. std::cout << bd << std::endl;
  37. }
  38.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
Base
Derived, Base
Derived, Base