#include <iostream>
#include <string>
#include <vector>
#include <map>
std::vector<std::pair<std::string, [type of the object, entity or banana]>> nep; //[type of the object, entity or banana] is my imaginary type
class Entity
{
private:
int x;
public:
Entity(const int x) : x(x) {};
int GetX() const { return x; };
};
class Banana
{
private:
int y;
public:
Banana(const int y) : y(y) {};
int GetY() const { return y; };
};
[type of the object, entity or banana] CreateObj(std::string name) //Used that imaginary variable here again
{
for (unsigned short int i = 0; i < nep.size(); i++)
{
if (nep.at(i).first == name)
{
return [new object with type = nep.at(i).second];
}
}
}
int main()
{
nep.push_back({ "ent", Entity });
nep.push_back({ "banan", Banana });
CreateObj(banan).GetY();
std::cin.get();
}
[type of the object, entity or banana] is my imaginary variable-type thing.
What I'd like to do is pass there a class for example, and then using CreateObj() function I'd like to create new object of that type and use it.
How can I do that?
enum classfor type safety rather than strings.