How can I call a function using a function pointer?

前端 未结 16 1615
孤独总比滥情好
孤独总比滥情好 2020-12-02 17:24

Suppose I have these three functions:

bool A();
bool B();
bool C();

How do I call one of these functions conditionally using a function poi

16条回答
  •  既然无缘
    2020-12-02 17:31

    I usually use typedef to do it, but it may be overkill, if you do not have to use the function pointer too often..

    //assuming bool is available (where I come from it is an enum)
    
    typedef bool (*pmyfun_t)();
    
    pmyfun_t pMyFun;
    
    pMyFun=A; //pMyFun=&A is actually same
    
    pMyFun();
    

提交回复
热议问题