简体   繁体   English

C++ 从函数指针数组调用函数

[英]C++ Calling Functions from an Array of Function Pointers

I'm using function pointers stored in an array with a typedef defining the pointer and I'm a little lost on how I'm supposed to call the function.我正在使用存储在数组中的函数指针,并带有定义指针的 typedef,但我对如何调用该函数有点迷茫。

here's the Menu.h part:这是 Menu.h 部分:

typedef void( Menu::*FunctionPointer )();

FunctionPointer* m_funcPointers;

here's the Menu.cpp part:这是 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 is an integer used as the index. m_currentButton 是用作索引的整数。 I'm not sure how to actually call the function as the above line gives me this error:我不确定如何实际调用该函数,因为上面的行给了我这个错误:

**C2064 term does not evaluate to a function taking 0 arguments**

and visual studio give me this :和视觉工作室给我这个:

expression preceding parentheses of apparent call must have (pointer-to-) function type

I don't know how to solve the above problem and whether it's due to how I'm calling the function or how I'm storing it.我不知道如何解决上述问题,也不知道是因为我调用函数的方式还是存储它的方式。 Thanks in advance.提前致谢。

Calling Functions from an Array of Function Pointers从函数指针数组调用函数

You call a function pointer in an array the same way as you would call a function that is not in an array.调用数组中的函数指针的方式与调用不在数组中的函数相同。

Your problem isn't how to call a function pointer in an array as such.您的问题不在于如何在数组中调用函数指针。 Your problem that you're trying to call a member function pointer as if it were a function pointer.你的问题是你试图调用一个成员函数指针,就好像它是一个函数指针一样。

You can call a member function pointer like this:您可以像这样调用成员函数指针:

Menu menu; // you'll need an instance of the class
(menu.*m_funcPointers[m_currentButton])();

Edit for the new example code: Since you're in a member function, perhaps you intend to call the member function pointer on this :编辑新的示例代码:由于您在成员函数中,因此您可能打算在this上调用成员函数指针:

(this->*m_funcPointers[m_currentButton])();

If you find the syntax painful to read, I won't blame you.如果你觉得语法读起来很痛苦,我不会责怪你。 Instead, I'll suggest using std::invoke instead (available since C++-17):相反,我建议使用std::invoke代替(自 C++-17 起可用):

std::invoke(m_funcPointers[m_currentButton], this);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM