我向一位学习型同事演示了如何使用函数指针,以及如何使用函数指针数组。我写下了下面的代码,以便他可以执行索引分派:

typedef void (*VoidFunction)();
VoidFunction functions[]  =
    {editProgramName,
     editProgramLength,
     editProgramCycles,
     editProgramNumberOfSets,
     editProgramEditSets,
     editProgramSave,
     editProgramCancel};
// now dispatch                    
functions[scroll.arrayFocusIndex]();

然后他问……“没有typedef怎么办?”在尝试了各种看似可行的方法之后,我发现自己根本没有什么线索。我在谷歌上找到的所有点击似乎总是使用typedef。有没有办法在不使用函数指针的typedef的情况下进行内联?

最佳答案

void (*functions[])() =
    {editProgramName,
     editProgramLength,
     editProgramCycles,
     editProgramNumberOfSets,
     editProgramEditSets,
     editProgramSave,
     editProgramCancel};
// now dispatch
functions[scroll.arrayFocusIndex]();

07-24 09:50
查看更多