I have a list of functions from which the user chooses one to be used. This function is then passed as a parameter for some other function. For example:
// Declare some functions here
int main() {
// Declare some pointer
int choice;
cout << "1 - functionA, 2 - functionB..." << endl;
cin >> choice;
switch (choice) {
case 1:
// Assign pointer to functionA address
break;
case 2: //etc
// Assign pointer to functionB address
break;
//etc
}
float x; float y; float z;
float value = BigFunc(//ChosenFunction, x, y, z);
cout << value << endl;
return 0;
}
float BigFunc(//ChosenFunction(returns a float), float x, float y, float z) {
// Some code
float a = OtherFunc(//ChosenFunction, x, y, z);
// Some code
return a;
}
float OtherFunc(//ChosenFunction, float x, float y, float z) {
for (//some for loop) {
return b += ChosenFunction(x);
}
return b;
}
I have used comments to indicate areas where some code would be. The functions from which the user can choose are functions which take 1 float argument, perform some mathematical operations on that value and then return it.
I have seen some examples online however they are all very specific and have not helped me very much. I have tried to keep the example in this post as general as possible so that hopefully others that stumble across it can learn from it too.
I have attempted to use pointers to accomplish this task however it has been to no avail. Online I have seen people using float (*pointer)(float)
however I am unfamiliar with using a pointer like this. I have also seen people using typedef
however once again I am unsure of how to implement that here.
Any help or pointers (pun) in the right direction would be much appreciated! Sean.
Edit: The question which this has been tagged as a duplicate of is similar however I did look at that question before and I felt that it does not sufficiently resolve this problem. The program which I am writing is more complicated than this case however I have kept it simpler so that others can find this post helpful after I am finished with it.