Using function pointer inside class

时间:2018-07-25 05:14:05

标签: c++ c++11

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.

0 个答案:

没有答案