fork download
  1. #include <iostream>
  2.  
  3. class Base {
  4. public:
  5. Base(int v) : x(v) {}
  6. virtual Base operator +(Base& other) {
  7. std::cout << "Base::operator +\n";
  8. return Base(x + other.x);
  9. }
  10. int x;
  11. };
  12.  
  13. class Derived: public Base {
  14. public:
  15. Derived(int v) : Base(v) {}
  16. Base operator +(Base& other) override {
  17. std::cout << "Derived::operator +\n";
  18. return Base(x + other.x);
  19. }
  20. };
  21.  
  22. Base polym(Base& left, Base& right) {
  23. return left + right;
  24. }
  25.  
  26. int main() {
  27. Base b(2);
  28. Derived d(3);
  29. polym(b, d);
  30. polym(d, b);
  31. return 0;
  32. }
Success #stdin #stdout 0.01s 5312KB
stdin
Standard input is empty
stdout
Base::operator +
Derived::operator +