#include <iostream>
class Base {
public:
Base(int v) : x(v) {}
virtual Base operator +(Base& other) {
std::cout << "Base::operator +\n";
return Base(x + other.x);
}
int x;
};
class Derived: public Base {
public:
Derived(int v) : Base(v) {}
Base operator +(Base& other) override {
std::cout << "Derived::operator +\n";
return Base(x + other.x);
}
};
Base polym(Base& left, Base& right) {
return left + right;
}
int main() {
Base b(2);
Derived d(3);
polym(b, d);
polym(d, b);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKY2xhc3MgQmFzZSB7CglwdWJsaWM6CglCYXNlKGludCB2KSA6IHgodikge30KCXZpcnR1YWwgQmFzZSBvcGVyYXRvciArKEJhc2UmIG90aGVyKSB7CgkJc3RkOjpjb3V0IDw8ICJCYXNlOjpvcGVyYXRvciArXG4iOwoJCXJldHVybiBCYXNlKHggKyBvdGhlci54KTsKCX0KCWludCB4Owp9OwoKY2xhc3MgRGVyaXZlZDogcHVibGljIEJhc2UgewoJcHVibGljOgoJRGVyaXZlZChpbnQgdikgOiBCYXNlKHYpIHt9CglCYXNlIG9wZXJhdG9yICsoQmFzZSYgb3RoZXIpIG92ZXJyaWRlIHsKCQlzdGQ6OmNvdXQgPDwgIkRlcml2ZWQ6Om9wZXJhdG9yICtcbiI7CgkJcmV0dXJuIEJhc2UoeCArIG90aGVyLngpOwoJfQp9OwoKQmFzZSBwb2x5bShCYXNlJiBsZWZ0LCBCYXNlJiByaWdodCkgewoJcmV0dXJuIGxlZnQgKyByaWdodDsKfQoKaW50IG1haW4oKSB7CglCYXNlIGIoMik7CglEZXJpdmVkIGQoMyk7Cglwb2x5bShiLCBkKTsKCXBvbHltKGQsIGIpOwoJcmV0dXJuIDA7Cn0=