I have specific code that I want to optimize. It looks like this:
function abc( string format ) {
if (format == "a") {
classx::a t;
doit(t);
}
if (format == "b"){
classx::b t;
doit(t);
}
if (format == "c"){
classx::c t;
doit(t)
}
if (format == "d"){
classx::d t;
doit(t);
}
}
Currently there are many doit () functions with a different type
function doit( classx:a ) {
different code for a
}
function doit( classx:b ) {
different code for b
}
... etc.
As you can see, a lot of code is replicated. However, I cannot figure out how to reduce words. Note that: doit (x) is overloaded with another type. The class a, b, c, d is derived from the class with the name "X".
I can create a pointer type classx :: X:
classx::X *t;
if (format == "a") t = new classx::a
if (format == "b") t = new classx::b
if (format == "c") t = new classx::c
if (format == "d") t = new classx::d
doit(*t)
but then you still need to write doit () for type classx :: X with a bunch of "if then" and apply to the correct type ... since C ++ cannot automatically detect and apply to the correct type.
I wonder if there is a faster / smarter way to do this. Thanks in advance.