44225

Passing a void function as an argument to another function

Question:

I'm trying to pass a void function to another void function, unsuccessfully so far. So I created this function inside the class called ExitButton like this. ExitButton.h:

class ExitButton{ void setup(void (*_setup)); };

Then I include that class into another class like this. ofApp.h:

include "ExitButton.h" class ofApp : public ofBaseApp{ void update(); void setup(); StartButton *startButton; }

So in my ofApp.cpp I want to call the update function like this:

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 }

So I assume, I can only pass a void function that is a pointer? Is it actually possible to pass a void function as a parameter to another function?

Answer1:

This is probably what you want:

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

Recommend

  • Passing a void function as an argument to another function
  • How to hide a taskbar when AutoHideTaskbar option selected and a script (ahk script) trying to hide
  • Integration issue with PyObjC and TKinter
  • Append jtextarea from another class
  • Reusing Django apps in own project wich require a lower django version
  • Get constraint's id programmatically
  • How to stop a loop with a button?
  • Sobel filter in x direction in CUDA
  • Detaching entity along with referenced entities
  • TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'
  • undefined reference error due to use of static variables [duplicate]
  • Opaque reference instead of PImpl. Is it possible?
  • Compiling dlib on OS X
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • retrieve vertices with no linked edge in arangodb
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Buffer size for converting unsigned long to string
  • Hits per day in Google Big Query
  • JTable with a ScrollPane misbehaving
  • Understanding cpu registers
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • How to get Windows thread pool to call class member function?
  • Add sale price programmatically to product variations
  • unknown Exception android
  • Busy indicator not showing up in wpf window [duplicate]
  • failed to connect to specific WiFi in android programmatically
  • Why do underscore prefixed variables exist?
  • How can I use threading to 'tick' a timer to be accessed by other threads?