I've read a number of topics about function pointers, and many of them appear to be exactly what I'm trying to do, but for some reason, I can't get what I want to do to work. Basically, it won't compile.
Here's my simple class:
class LessSimple
{
public:
LessSimple ();
~LessSimple () {}
int
LessSimple::doFunc (const char *f, int val)
{
int i;
for (i = 0; i < 3; ++i)
{
if (std::strcmp (f, methods[i].name) == 0)
{
return (*methods[i].method)(this, val);
}
}
return -1;
}
For whatever reason, I get an error in LessSimple::LessSimple() on the 3 lines that set the value for methods[N].method. Here's the error:
methodPtr.C: In constructor 'LessSimple::LessSimple()':
methodPtr.C:142:38: error: cannot convert 'int (LessSimple::*)(int)' to 'LessSimple::mthd {aka int (*)(int)}' in assignment
If I change the definition of 'mthd' to:
typedef int (*LessSimple::mthd)(int);
The compiler errors saying:
methodPtr.C:55:19: error: extra qualification 'LessSimple::' on member 'mthd' [-fpermissive]
methodPtr.C:55:40: error: typedef name may not be a nested-name-specifier
So, I'm lost.
I'm testing this on Fedora 17 using the standard g++ compiler.