函数指针堆栈:如何调用函数?

发布于 2024-11-17 22:51:47 字数 1311 浏览 6 评论 0原文

我有一堆函数指针(都是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最初的梦 2024-11-24 22:51:47

您需要一个实例来调用成员函数。试试这个:

InstructionScreen screen;
MemberFuncPtr step = instructionStep.top();
(screen.*step)();

要从另一个成员函数中运行堆栈中的函数,您可以使用:

MemberFuncPtr step = instructionStep.top();
(this->*step)();

You need an instance to call a member function. Try this:

InstructionScreen screen;
MemberFuncPtr step = instructionStep.top();
(screen.*step)();

To run a function in the stack from within another member function, you can use:

MemberFuncPtr step = instructionStep.top();
(this->*step)();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文