#include <iostream>
#include <map>
#include <string>
struct A {
virtual ~A(){}
};
struct B : A
{
B()
{
std::cout << "B constructed\n";
}
virtual ~B()
{
std::cout << "B destructed\n";
}
};
struct C : A
{
C()
{
std::cout << "C constructed\n";
}
virtual ~C()
{
std::cout << "C destructed\n";
}
};
template <class T>
A* create()
{
return new T;
}
typedef A*(*CreateFN)();
int main()
{
std::map<std::string, CreateFN> classMap;
classMap["B"] = &create<B>;
classMap["C"] = &create<C>;
delete classMap["B"]();
delete classMap["C"]();
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWFwPgojaW5jbHVkZSA8c3RyaW5nPgoKc3RydWN0IEEgewogICAgICAgIHZpcnR1YWwgfkEoKXt9Cn07CgpzdHJ1Y3QgQiA6IEEKewogICAgICAgIEIoKQogICAgICAgIHsKICAgICAgICAgICAgICAgIHN0ZDo6Y291dCA8PCAiQiBjb25zdHJ1Y3RlZFxuIjsKICAgICAgICB9CgogICAgICAgIHZpcnR1YWwgfkIoKQogICAgICAgIHsKICAgICAgICAgICAgICAgIHN0ZDo6Y291dCA8PCAiQiBkZXN0cnVjdGVkXG4iOwogICAgICAgIH0KfTsKCnN0cnVjdCBDIDogQQp7CiAgICAgICAgQygpCiAgICAgICAgewogICAgICAgICAgICAgICAgc3RkOjpjb3V0IDw8ICJDIGNvbnN0cnVjdGVkXG4iOwogICAgICAgIH0KCiAgICAgICAgdmlydHVhbCB+QygpCiAgICAgICAgewogICAgICAgICAgICAgICAgc3RkOjpjb3V0IDw8ICJDIGRlc3RydWN0ZWRcbiI7CiAgICAgICAgfQp9OwoKdGVtcGxhdGUgPGNsYXNzIFQ+CkEqIGNyZWF0ZSgpCnsKICAgICAgICByZXR1cm4gbmV3IFQ7Cn0KCnR5cGVkZWYgQSooKkNyZWF0ZUZOKSgpOwoKCmludCBtYWluKCkKewogICAgICAgIHN0ZDo6bWFwPHN0ZDo6c3RyaW5nLCBDcmVhdGVGTj4gY2xhc3NNYXA7CiAgICAgICAgY2xhc3NNYXBbIkIiXSA9ICZjcmVhdGU8Qj47CiAgICAgICAgY2xhc3NNYXBbIkMiXSA9ICZjcmVhdGU8Qz47CgogICAgICAgIGRlbGV0ZSBjbGFzc01hcFsiQiJdKCk7CiAgICAgICAgZGVsZXRlIGNsYXNzTWFwWyJDIl0oKTsKfQo=