I would like to replace big switch with something more elegant.
class Base
{
public:
Base(void*data, int size);
virtu
If the constructors are exactly the same and Something
methods are called similarly then you should be able to use templates like this:
template
void DoSomething(void*data, int size){
T t(data, size);
t.Something();
}
..
{
switch(input){
case 'a': DoSomething(..); break;
case 'b': DoSomething(..); break;
}
}
you can use is_base_of if you want to validate that the template is a derived class of Base
.
Since you switch on an unknown variable (in this case a char
) I'm not sure how you would minimize the switch, unless following the pattern suggested by Alan Birtles.