How do I store a function to a variable?

前端 未结 7 1902
灰色年华
灰色年华 2020-12-08 08:36

I think they are called functors? (it\'s been a while)

Basically, I want to store a pointer to a function in a variable, so I can specify what function I want to use

7条回答
  •  遥遥无期
    2020-12-08 09:01

    unsigned int (* myFuncPointer)(unsigned int) = &func_1;
    

    However, the syntax for function pointers is awful, so it's common to typedef them:

    typedef unsigned int (* myFuncPointerType)(unsigned int);
    myFuncPointerType fp = &func_1;
    

提交回复
热议问题