我使用存储在数组中的函数指针,并使用 typedef 定义指针,但我对如何调用该函数有点迷失。
这是 Menu.h 部分:
typedef void( Menu::*FunctionPointer )();
FunctionPointer* m_funcPointers;
这是 Menu.cpp 部分:
Menu::Menu()
: m_running( true )
, m_frameChanged( true )
, m_currentButton( 0 )
, m_numOfButtons( k_maxButtons )
, m_menuButtons( new MenuButton[k_maxButtons] )
, m_nullBtn( new MenuButton( "null", Vector2( -1, -1 ) ) )
, m_frameTimer( 0 )
, m_funcPointers( new FunctionPointer[k_maxButtons])
{
m_timer.start();
clearButtons();
mainMenu();
}
void Menu::enterButton()
{
m_funcPointers[m_currentButton]();//Error here
}
void Menu::mainMenu()
{
m_funcPointers[0] = &Menu::btnPlay;
m_menuButtons[0] = MenuButton("Play", Vector2(0, 0));
m_funcPointers[1] = &Menu::btnHiScores;
m_menuButtons[1] = MenuButton("HiScores", Vector2(0, 1));
m_funcPointers[2] = &Menu::btnExit;
m_menuButtons[2] = MenuButton("Exit", Vector2(0, 2));
}
void Menu::btnPlay()
{
StandardGame* game = new StandardGame();
game->play();
delete game;
}
m_currentButton 是一个用作索引的整数。我不确定如何实际调用该函数,因为上面的行给了我这个错误:
**C2064 term does not evaluate to a function taking 0 arguments**
视觉工作室给了我这个:
expression preceding parentheses of apparent call must have (pointer-to-) function type
我不知道如何解决上述问题,也不知道这是否是由于我如何调用该函数或如何存储它而导致的。
提前致谢。