Hi,
As part of a simulation program, I have several different model
classes, ModelAA, ModelBB, etc., which are all derived from the class
BasicModel by inheritance.
model to use, for example if the parameter model_name is "aa", then
choose ModelAA.
Currently I do this as follows:
BasicModel* model;
if (model_name == "aa") {
model = new ModelAA(a,b,c);
}
else if (model_name == "bb") {
model = new ModelBB(a,b,c);
}
etc., through all the (many) models.
Each model constructor takes the same parameters, if this is of any
help.
Once this is done, only virtual functions in BasicModel are called.
(But the implementation of these can be very different in the different
kinds of model.)
It is clear to me that there must be a better way of initiating the
models, which is easier to update when a new model is added, e.g.
involving some kind of array or map, mapping the model_name string to
the class name, but I can't work out the syntax (I am somewhat of a
newbie).
I have looked hard at the FAQ, and I cannot see anything that is
relevant (but please correct me if I'm wrong). Any suggestions or
pointers would be greatly appreciated.
Thanks and best wishes,
David.
As part of a simulation program, I have several different model
classes, ModelAA, ModelBB, etc., which are all derived from the class
BasicModel by inheritance.
>From a command-line parameter, I need to choose the correct type of
choose ModelAA.
Currently I do this as follows:
BasicModel* model;
if (model_name == "aa") {
model = new ModelAA(a,b,c);
}
else if (model_name == "bb") {
model = new ModelBB(a,b,c);
}
etc., through all the (many) models.
Each model constructor takes the same parameters, if this is of any
help.
Once this is done, only virtual functions in BasicModel are called.
(But the implementation of these can be very different in the different
kinds of model.)
It is clear to me that there must be a better way of initiating the
models, which is easier to update when a new model is added, e.g.
involving some kind of array or map, mapping the model_name string to
the class name, but I can't work out the syntax (I am somewhat of a
newbie).
I have looked hard at the FAQ, and I cannot see anything that is
relevant (but please correct me if I'm wrong). Any suggestions or
pointers would be greatly appreciated.
Thanks and best wishes,
David.
Comment