C++ 从函数指针数组调用函数
C++ Calling Functions from an Array of Function Pointers
我正在使用存储在数组中的函数指针,并使用 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**
和visual studio给我这个:
expression preceding parentheses of apparent call must have (pointer-to-) function type
我不知道如何解决上述问题,也不知道是我调用函数的方式还是存储方式的问题。
提前致谢。
Calling Functions from an Array of Function Pointers
调用数组中的函数指针的方式与调用不在数组中的函数的方式相同。
您的问题不在于如何调用数组中的函数指针。你的问题是你试图调用一个成员函数指针,就好像它是一个函数指针一样。
你可以这样调用成员函数指针:
Menu menu; // you'll need an instance of the class
(menu.*m_funcPointers[m_currentButton])();
编辑新示例代码:由于您在成员函数中,也许您打算在 this
:
上调用成员函数指针
(this->*m_funcPointers[m_currentButton])();
如果您觉得语法难以阅读,我不会怪您。相反,我建议改用 std::invoke
(自 C++-17 起可用):
std::invoke(m_funcPointers[m_currentButton], this);
我正在使用存储在数组中的函数指针,并使用 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**
和visual studio给我这个:
expression preceding parentheses of apparent call must have (pointer-to-) function type
我不知道如何解决上述问题,也不知道是我调用函数的方式还是存储方式的问题。 提前致谢。
Calling Functions from an Array of Function Pointers
调用数组中的函数指针的方式与调用不在数组中的函数的方式相同。
您的问题不在于如何调用数组中的函数指针。你的问题是你试图调用一个成员函数指针,就好像它是一个函数指针一样。
你可以这样调用成员函数指针:
Menu menu; // you'll need an instance of the class
(menu.*m_funcPointers[m_currentButton])();
编辑新示例代码:由于您在成员函数中,也许您打算在 this
:
(this->*m_funcPointers[m_currentButton])();
如果您觉得语法难以阅读,我不会怪您。相反,我建议改用 std::invoke
(自 C++-17 起可用):
std::invoke(m_funcPointers[m_currentButton], this);