#include <iostream>
template <typename T>
class Base
{
public:
void method() {
static_cast<T*>(this)->method();
}
};
class Derived1 : public Base<Derived1>
{
public:
void method() {
std::cout << "Derived1 method" << std::endl;
}
};
class Derived2 : public Base<Derived2>
{
public:
void method() {
std::cout << "Derived2 method" << std::endl;
}
};
int main()
{
Base<Derived1> *d1 = new Derived1;
Base<Derived2> *d2 = new Derived2;
d1->method();
d2->method();
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKdGVtcGxhdGUgPHR5cGVuYW1lIFQ+CmNsYXNzIEJhc2UKewpwdWJsaWM6CiAgICB2b2lkIG1ldGhvZCgpIHsKICAgICAgICBzdGF0aWNfY2FzdDxUKj4odGhpcyktPm1ldGhvZCgpOwogICAgfQp9OwoKY2xhc3MgRGVyaXZlZDEgOiBwdWJsaWMgQmFzZTxEZXJpdmVkMT4KewpwdWJsaWM6CiAgICB2b2lkIG1ldGhvZCgpIHsKICAgICAgICBzdGQ6OmNvdXQgPDwgIkRlcml2ZWQxIG1ldGhvZCIgPDwgc3RkOjplbmRsOwogICAgfQp9OwoKCmNsYXNzIERlcml2ZWQyIDogcHVibGljIEJhc2U8RGVyaXZlZDI+CnsKcHVibGljOgogICAgdm9pZCBtZXRob2QoKSB7CiAgICAgICAgc3RkOjpjb3V0IDw8ICJEZXJpdmVkMiBtZXRob2QiIDw8IHN0ZDo6ZW5kbDsKICAgIH0KfTsKCgppbnQgbWFpbigpCnsKICAgIEJhc2U8RGVyaXZlZDE+ICpkMSA9IG5ldyBEZXJpdmVkMTsKICAgIEJhc2U8RGVyaXZlZDI+ICpkMiA9IG5ldyBEZXJpdmVkMjsKICAgIGQxLT5tZXRob2QoKTsKICAgIGQyLT5tZXRob2QoKTsKICAgIHJldHVybiAwOwp9