将void函数作为参数传递给另一个函数

时间:2014-09-16 03:20:32

标签: c++ function void openframeworks

我试图将void函数传递给另一个void函数,到目前为止还没有成功。所以我在这个名为ExitButton的类中创建了这个函数。 ExitButton.h:

class ExitButton{

 void setup(void (*_setup));

};

然后我将该类包含在另一个类中。 ofApp.h:

include "ExitButton.h"
class ofApp : public ofBaseApp{

 void update();
 void setup(); 
 StartButton *startButton;

}

所以在我的ofApp.cpp中我想调用这样的更新函数:

void ofApp::update(){


exitButton->setup(setup()); // This throws me the following error: Cannot initialize a parameter of type 'void (*)' with an rvalue of type void
    }

所以我假设,我只能传递一个指针的void函数? 实际上是否可以将void函数作为参数传递给另一个函数?

1 个答案:

答案 0 :(得分:2)

这可能是你想要的:

#include <iostream>
using namespace std;

class ExitButton{
public:
    void setup(void (*_setup)())
    {    
        _setup(); // we call the function pointer
    };
};    


void setup() // this is a void function
{
    cout << "calling void setup()" << endl;
}

int main()
{
    ExitButton eb;
    eb.setup(setup); // use a void function as a parameter
}
相关问题