c++ choose a certain function depending on variable?
Hi,
I'm trying to choose between what function should be used depending on a variable. The situation is that I have some cases I, II and III and they provide different functions to my program (with the same interface). Thus I'd like to have a switch to detect which set of functions I want to use.
I thought of something like this (all in some pseudo c++)
int main()
{
...
// determine what set of functions I want to have
selector=I; // or II or III
FunctionSelector FS(selector);
...
output1=FS.function1(x);
output2=FS.function2(x,y);
}
And the function selector class would look something like
class FunctionSelector {
public:
// constructor
FunctionSelector(Selector s) {
switch(s) {
case I:
// definition of function1 and function2 for this case
double foo1(double x) { return x*x; }
double foo2(double x,double y) { return x*y; }
privfcn1=&foo1;
privfcn2=&foo2;
case II:
// similar
case III:
// similar
}
// The interface
double function1(double x){return privfcn1(x);}
double function2(double x,double y) {return privfcn2(x,y); }
private:
double (*privfcn1)(double);
double (*privfcn2)(double,double);
};
There are a bunch of issues with this... I can't declare functions in the switch/case statement for example. My main motivation for using this method is that I could really get a good overview of my code, all functions and stuff concerning a certain selector would be defined in a single place. This is my main motivation. Any other ideas how to do this?
Thanks!
|