我不熟悉可变参数模板和压缩参数等等。 我想在我的程序中有一个“实体组件系统”,在尝试向实体添加组件时,我开始意识到我的尝试有一个重大缺陷。
无论如何这是我失败的尝试。
struct A{
float x;
float y;
A(float _x, float _y):x(_x), y(_y){}
};
struct B{
float x;
float y;
float z;
B(float _x, float _y, float _z):x(_x), y(_y), z(_z){}
};
struct Entity{
A* a = NULL;
B* b = NULL;
Entity(){}
template<typename T, typename... args>
void Add(args... _args){
if(typeid(T) == typeid(A))
a = new A(_args...);
else if(typeid(T) == typeid(B))
b = new B(_args...);
else
throw("Invalid component added");
}
};
实现看起来像这样..
Entity ent;
ent.Add<A>(12.24f, 123.246f);
ent.Add<B>(1.f, 1.2f, 1.23f);
我希望实现以某种方式工作..必须为此更改什么??