gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:41:24 32 4
gpt4 key购买 nike

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

class ExitButton{

void setup(void (*_setup));

};

然后我像这样将那个类包含到另一个类中。的App.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 函数作为参数传递给另一个函数?

最佳答案

这可能是您想要的:

#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
}

关于c++ - 将 void 函数作为参数传递给另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25860157/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com