Why is the following code results in error :
class A {
public:
typedef void (A::*funptr)(void);
void fun(void ) {
cout << " Fun Call " <<endl;
}
void foo(void ) {
cout << " Foo Call " <<endl;
}
funptr p[2];
funptr q;
A()
{
p[0]=&A::foo;
p[1]=&A::fun;
q =&A::fun;
}
};
int main ()
{
A obj;
(obj.*q)(void);
//(obj.p[0])();
//(obj.p[1])();
return 0;
}
You will need to call it like this:
The .* operator doesn’t take a member name on the right-hand side, but rather an expression that evaluates to a member pointer. When you write this:
It is looking for a variable called q, but there is no such variable in scope.