Can’t get it.
Using g++ compiler.
Code:
#include <iostream>
using namespace std;
typedef void (* FPTR) ();
class Test
{
void f1()
{
cout << "Do nothing 1" << endl;
}
void f2()
{
cout << "Do nothing 2" << endl;
}
static FPTR const fa[];
};
FPTR const Test::fa[] = {f1, f2};
Error:
test.cpp:22: error: argument of type ‘void (Test::)()’ does not match ‘void (* const)()’
test.cpp:22: error: argument of type ‘void (Test::)()’ does not match ‘void (* const)()’
I just want to obtain constant array of function pointers, so
fa[0] = f2;
will cause an error like ‘modifying read-only member Test::fa’
The compiler is right. The pointer type is
void (Test::*)(). Try it:f1andf2are not functions (i.e. free functions), but (non-static) member functions. Those are very different animals: You can call a function, but you cannot just call a member function. You can only call a member function on an instance object, and anything else doesn’t make sense.