This question already has an answer here:
- Function pointer to member function 5 answers
I am trying to use function pointer inside a class to activate one of the many private functions available in the class. I wrote following sample code but compiler asks me to make private functions static. If I make it static I will loose my purpose of doing this. Is there any other way to do this? My compiler supports c++11 std. Below is my sample code.
typedef void (*p_t)(void);
class x{
inline void printint(){
int b=10;
std::cout<<"Int" << b << std::endl;
}
inline void printfloat(){
float b=10;
std::cout<<"Float" << b << std::endl;
}
private:
public:
p_t p;
void set_i(void) { p = &printint;}
void set_f(void) { p = &printfloat;}
};
int main()
{
x<float> n;
n.set_i();
n.p();
n.set_f();
n.p();
return 0;
}
Thanks.
Aucun commentaire:
Enregistrer un commentaire