#include <iostream>
#include <string>
#include <map>
#include <functional>
using namespace std;
struct BaseClass {virtual void foo()=0;};
struct DerivedClass1 : public BaseClass {void foo() {cout << "1" << endl;}};
struct DerivedClass2 : public BaseClass {void foo() {cout << "2" << endl;}};
map<string,function<BaseClass*()> > factory {
{"one", [](){return new DerivedClass1();}}
, {"two", [](){return new DerivedClass2();}}
};
int main() {
BaseClass *a = factory["one"]();
BaseClass *b = factory["two"]();
a->foo();
b->foo();
delete a;
delete b;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8bWFwPgojaW5jbHVkZSA8ZnVuY3Rpb25hbD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpzdHJ1Y3QgQmFzZUNsYXNzIHt2aXJ0dWFsIHZvaWQgZm9vKCk9MDt9OwpzdHJ1Y3QgRGVyaXZlZENsYXNzMSA6IHB1YmxpYyBCYXNlQ2xhc3Mge3ZvaWQgZm9vKCkge2NvdXQgPDwgIjEiIDw8IGVuZGw7fX07CnN0cnVjdCBEZXJpdmVkQ2xhc3MyIDogcHVibGljIEJhc2VDbGFzcyB7dm9pZCBmb28oKSB7Y291dCA8PCAiMiIgPDwgZW5kbDt9fTsKCm1hcDxzdHJpbmcsZnVuY3Rpb248QmFzZUNsYXNzKigpPiA+IGZhY3RvcnkgewogICAgeyJvbmUiLCBbXSgpe3JldHVybiBuZXcgRGVyaXZlZENsYXNzMSgpO319CiwgICB7InR3byIsIFtdKCl7cmV0dXJuIG5ldyBEZXJpdmVkQ2xhc3MyKCk7fX0KfTsKCmludCBtYWluKCkgewogICAgQmFzZUNsYXNzICphID0gZmFjdG9yeVsib25lIl0oKTsKICAgIEJhc2VDbGFzcyAqYiA9IGZhY3RvcnlbInR3byJdKCk7CiAgICBhLT5mb28oKTsKICAgIGItPmZvbygpOwogICAgZGVsZXRlIGE7CiAgICBkZWxldGUgYjsKCXJldHVybiAwOwp9