The idea is to call different functions based on their index in table. But line 27 generates the runtime error (( I tried to fix but didn't succeed ((( Here's the simplified code:
#include <iostream>
void f1 (void) {
std::cout << "f1" << "\n";
}
void f2 (void) {
std::cout << "f2" << "\n";
}
typedef void (*fPtr[3])(void); // simple "[]" instead of "[3]" gets the compile error
class modeChanger {
public:
modeChanger (fPtr funcArray);
void op ();
private:
fPtr *_funcArray;
};
modeChanger::modeChanger (fPtr funcArray) {
_funcArray = (fPtr *) funcArray;
}
void modeChanger::op () {
(*_funcArray[0])();
(*_funcArray[1])(); // Line 27: this line generates a runtime error! Just comment it to get all work
}
void (*modeFuncArray[])(void) = {f1, f2, f2};
modeChanger *mode = new modeChanger (modeFuncArray);
int main() {
(*modeFuncArray[1])(); // Works fine
mode->op(); // generates a runtime error
return 0;
}
This works good:
(*_funcArray[0])();
as well as this:
(*modeFuncArray[1])();
but this generates a runtime error...
(*_funcArray[1])();
Seems that incrementing of _funcArray is incorrect for some reason.
_funcArray = (fPtr *) funcArray;)std::functioninstead. That will make much of your code simpler. Then if you use a standard container likestd::vectororstd::arraythen you simplify even further. Simple code have less chance of errors or problems.