class Factory
{
public:
Base* create() { return new Rand();}
Base* create(double value) { return new Op(value); }
Base* create(const std::string& comp, Base* x, Base* y)
{
/*
std::map<std::string, Base*> map_CreateFuncs = {
{ "+", new Add(x,y) },
{ "-", new Sub(x,y) },
{ "*", new Mult(x,y) },
{ "/", new Div(x,y) },
{ "**", new Pow(x,y) }
};
return (map_CreateFuncs.count(comp) == 1) ? map_CreateFuncs[comp] : nullptr;
*/
if (comp == "+") return new Add(x,y);
else if (comp == "-") return new Sub(x,y);
else if (comp == "*") return new Mult(x,y);
else if (comp == "/") return new Div(x,y);
else if (comp == "**") return new Pow(x,y);
else return nullptr;
}
};
Using the if/elseif's works ~ no memory leaks. But I'd like to use the map method that I started with.
Wondering what the proper way to implement my create(string, Base*, Base*) is, using the map.
I've read code out there that uses a typedef but I couldn't understand its implementation.