是否有可能从配置中控制不同类的创建?
说,我有一个硬编码的函数:
BaseClass* getClassObject(int type)
{
switch (type)
{
case 1:
return new DerivedClass1();
case 2:
return new DerivedClass2();
default:
return 0;
}
}
是否可以将控制权转移到某种数据结构中,以便用户只需要填写数据结构来控制函数的行为方式?