函数指针堆栈:如何调用函数?
我有一堆函数指针(都是 void 类型且没有参数)。我很难找出如何调用/执行堆栈中的函数?
如果你看一下下面的简单例子,一切都会编译&除了最后一行之外都有效
typedef class InstructionScreen;
typedef void (InstructionScreen::*MemberFuncPtr)();
stack <MemberFuncPtr> instructionStep; // This is how I declare it. Works
instructionStep.push( &InstructionScreen::step1 ); // This is how I add the member function step(). Works
(*instructionStep.top())(); // How do I call the function now? This doesn't work
这是我试图编译的整个代码:
class InstructionScreen
{
public:
InstructionScreen()
{
instructionStep.push( &InstructionScreen::step1 );
instructionStep.push( &InstructionScreen::step2 );
// add timer to call run instructions each 10 seconds
}
void step1()
{
}
void step2()
{
}
void runInstructions()
{
if ( !instructionStep.empty() )
{
*(instructionStep.top())();
instructionStep.pop();
}
// else kill timer
}
private:
stack <MemberFuncPtr> instructionStep;
};
I have a stack of function pointers (all of void type & with no parameters). I am having difficulty finding out how I then call/execute a function that is in the stack?
If you look at the simple example below everthing compiles & works except for the last line
typedef class InstructionScreen;
typedef void (InstructionScreen::*MemberFuncPtr)();
stack <MemberFuncPtr> instructionStep; // This is how I declare it. Works
instructionStep.push( &InstructionScreen::step1 ); // This is how I add the member function step(). Works
(*instructionStep.top())(); // How do I call the function now? This doesn't work
This is the whole code I am attempting to get to compile:
class InstructionScreen
{
public:
InstructionScreen()
{
instructionStep.push( &InstructionScreen::step1 );
instructionStep.push( &InstructionScreen::step2 );
// add timer to call run instructions each 10 seconds
}
void step1()
{
}
void step2()
{
}
void runInstructions()
{
if ( !instructionStep.empty() )
{
*(instructionStep.top())();
instructionStep.pop();
}
// else kill timer
}
private:
stack <MemberFuncPtr> instructionStep;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要一个实例来调用成员函数。试试这个:
要从另一个成员函数中运行堆栈中的函数,您可以使用:
You need an instance to call a member function. Try this:
To run a function in the stack from within another member function, you can use: