I'm trying to implement a class in order to use function pointers to replace the previous enum-switch structure. This has been done by embedding the function pointer as part of the constructor.
I think I have the right code for the header and source files, but I don't know how to create an instance of the modified class as the function the function pointer points to is not a member function of the class.
So where previously the enum-switch structure allowed me to create an instance of PayOff with
PayOff callPayOff(Strike, call); as call is a defined enum data type.
How do I create an instance of PayOff with the function pointer structure?
eg. PayOff callPayOff(Strike, &call); doesn't work as call() is kept to be not a member function of PayOff to be consistent with the Open-Closed Principle.
1) Using the same name for the parameter and the function member (FunctionPtr) in the constructor caused the function pointer to be uninitialized. Changing the argument name to _FunctionPtr fixed this.
2) The 2nd argument is wrong in PayOff callPayOff(Strike, &call);. The address of a function is given by the functions name. Omit the & in &call. Use PayOff callPayOff(Strike, call); instead.
Also, you must declare the function max() somewhere before defining the call() and put() functions since they both call max().
With those fixes I found that your code works fine.
I think I've made the changes as you've suggested - it this right as below?
Re max(), I've included <minmax.h> from the standard library.
For 2) I've omitted the '&' so that when I create the instance of PayOff, it reads:
PayOff callPayOff(Strike, call);
But the compiler is still saying that 'call' is an undeclared identifier, or if I use 'PayOff::call', call is not a member function of PayOff.
I note that I have created the PayOff instance in main(), which is in a separate file to PayOff1.h and PayOff1.cpp. Does this make a difference? What am I missing?
1) You have put the definition of the call() in the PayOff.cpp file but did not put a prototype in PayOff.h. The function will not be recognized in main() even if you have included the line #include "PayOff.h" in the main file. Just as with the member functions you need to put the function prototype in the .h file and the definition in the .cpp file.
2) The assignment in the constructor FunctionPtr = FunctionPtr;. I didn't see this at first. When I fixed the compile errors the program ran but CRASHED. FunctionPtr is still uninitialized in that line. It needs to be FunctionPtr = FunctionPtr_;.
I would put the definitions for the call() and put() functions in the main file since they are not PayOff class member functions, but it still works if placed in the PayOff class files.
I have your code working. Here is exactly what I have:
#include <iostream>
usingnamespace std;
bool testFunct1(){cout << "Test 1" << endl; returntrue;}
bool testFunct2(){cout << "Test 2" << endl; returntrue;}
bool testFunct3(){cout << "Test 3" << endl; returntrue;}
typedefbool (*TestFuncT_ptr)();
class TestManager
{
public:
// It is easy to declare a function that receive function pointers
void AddTestAtIndex(TestFuncT_ptr ptr, int index)
{
test_functions[index] = ptr;
}
// It is easy to declare a function that returns function pointers
TestFuncT_ptr GetTestAtIndex(int index)
{
return test_functions[index];
}
void RunTests()
{
for(int index = 0; index < 3;++index)
{
test_functions[index]();
}
}
private:
// It is easy to have an array of function pointers
TestFuncT_ptr test_functions[3];
};
int main(int argc, char *argv[])
{
TestManager tm;
tm.AddTestAtIndex(testFunct1,0);
tm.AddTestAtIndex(testFunct2,1);
tm.AddTestAtIndex(testFunct3,2);
tm.RunTests();
// It is easy to declare a function pointers
TestFuncT_ptr ptr;
ptr = tm.GetTestAtIndex(0);
ptr();
return EXIT_SUCCESS;
}
Sorry for the slow responses (must be in a totally different time zone).
At line 39, what does "int argc, char *argv[]" do?
That is how command line parameters are passed into the program. It is force of habit that I write my main likeint main(int argc, char *argv[]).
At line 52, what does "ptr()" do?
it calls the function that is pointed to by ptr. As the function takes no parameters, there is no arguments in the parenthesis (and I'm not doing anything with the return value).
ok this is starting to sink in now, I was getting confused with what was getting initialised with what (really shouldn't name everything functionPtr!)
It does get fun when you are trying to name a type, variable and parameter. There are ways to use the same name for the member data and the parameter but that is probably best left for another time (read around scope and scope resolution).
so int argc, char *argv[] isn't actually necessary for the above program to run?
No it isn't required for it to run. When I said it was a habit I was not meaning to suggest that it is a good habit to get into, just that I nearly always write programs that use command line parameters to set various things so I just end up typing the full version of main.