This is the c ++ source I wrote. What I want to do is run init(), a member function of class A, and get_func returns the address of a function in a(), b(), c(), d(). Finally, func () is executed and I hope to see b. It seems very simple, but now this problem is bothering me.
The compiler gives me the following error: - 'choice' was not declared in this scope - 'a' was not declared in this scope return a (); - no matching function for call to 'A :: init (char)' a.init ('b');
What am i missing?
#include <iostream>
class A {
private:
int (A::*get_func())();
int a(){printf("a");}
int b(){printf("b");}
int c(){printf("c");}
int d(){printf("d");}
public:
A();
~A();
int init();
char choice;
}
A::init(char ch) {
this->choice = ch;
int (A::*func)() = get_func();
func();
}
int (A::*get_func())() {
switch(choice) {
case 'a' :
return a;
case 'b' :
return b;
case 'c' :
return c;
case 'd' :
return d;
}
}
int main() {
A a;
a.init('b');
}
get_funcsupposed to return? What happens with e.g.return a();? What is really returned?std::functiontype.