Originally Posted by
MK27
I get a rather baffling error for the line in red:
error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘func (...)’, e.g. ‘(... ->* func) (...)’
Okay, I got that to work (need parantheses around identifier):
Code:
#include <iostream>
using namespace std;
class X {
public:
X() {};
int foo (int a, int b) { return a+b; }
};
int main(int argc, char *argv[]) {
X a;
int(X::*func)(int,int) = &X::foo;
cout << (a.*func)(1,2) << endl;
return 0;
}
But I cannot get it to work the way you want:
Code:
#include <iostream>
using namespace std;
class X {
public:
X() {};
int foo (int a, int b) { return a+b; }
void test (X *obj, int(X::*func)(int,int)) {
cout (obj->*func)(1,2);
}
};
int main(int argc, char *argv[]) {
X a, b;
int(X::*func)(int,int) = &X::foo;
a.test(&b, func);
return 0;
}
test.cpp: In member function ‘void X::test(X*, int (X::*)(int, int))’:
test.cpp:10:20: error: invalid use of non-static member function
[Edit: error is actually caused by missing operator to cout in X::test ]