Skip to main content
replaced http://arduino.stackexchange.com/ with https://arduino.stackexchange.com/
Source Link

I am trying to pass a class internal function as a callback function within the class. The error behavior is similar to this problemthis problem. Whatsoever, I was unable to construct working code based on that question's answer.

I am trying to pass a class internal function as a callback function within the class. The error behavior is similar to this problem. Whatsoever, I was unable to construct working code based on that question's answer.

I am trying to pass a class internal function as a callback function within the class. The error behavior is similar to this problem. Whatsoever, I was unable to construct working code based on that question's answer.

bookkeeping
Source Link
Faultier
  • 143
  • 1
  • 7

fun times with polymorphism this

This is whatcode we discussed during looking for a solution. I tried till nowleave it here for future reference. 

I also tried creating an instance of this wrapper within the actual class to hold the callback, but that did not work either. Sorry I am a bit cumbersome with C++, coming from another language and apparently bit off a bit more than I could chew...

fun times with polymorphism this is what I tried till now. I also tried creating an instance of this wrapper within the actual class to hold the callback, but that did not work either. Sorry I am a bit cumbersome with C++, coming from another language and apparently bit off a bit more than I could chew...

fun times with polymorphism

This is code we discussed during looking for a solution. I leave it here for future reference. 

I tried creating an instance of this wrapper within the actual class to hold the callback, but that did not work either.

for discussion and latest comment
Source Link
Faultier
  • 143
  • 1
  • 7

For discussion with Michail

class CallbackWrapper
{
  public:
  void (*myCallback)();
  void call()
  { myCallback(); }

  virtual void setCallback(void (*callback)())
  {  }
};

class MyClass : public CallbackWrapper
{
  public:
  void setCallback(void (*callback)())
    { myCallback = callback; }
  void do_this() { Serial.println("doing this"); }
  void do_that() { Serial.println("doing that"); }
  void call() 
  { 
    CallbackWrapper::call();
    if (rand() > 0.5)
    { setCallback(&MyClass::do_this); }
    else
    { setCallback(&MyClass::do_that); }
  }
   MyClass()
  {  }
};

For discussion with Michail

class CallbackWrapper
{
  public:
  void (*myCallback)();
  void call()
  { myCallback(); }

  virtual void setCallback(void (*callback)())
  {  }
};

class MyClass : public CallbackWrapper
{
  public:
  void setCallback(void (*callback)())
    { myCallback = callback; }
  void do_this() { Serial.println("doing this"); }
  void do_that() { Serial.println("doing that"); }
  void call() 
  { 
    CallbackWrapper::call();
    if (rand() > 0.5)
    { setCallback(&MyClass::do_this); }
    else
    { setCallback(&MyClass::do_that); }
  }
   MyClass()
  {  }
};
why I want to change callbacks dynamically
Source Link
Faultier
  • 143
  • 1
  • 7
Loading
added more not working code; all glory to the wall of text!
Source Link
Faultier
  • 143
  • 1
  • 7
Loading
to faciliate discussion, I added a solution trial - probably temprorary.
Source Link
Faultier
  • 143
  • 1
  • 7
Loading
Source Link
Faultier
  • 143
  • 1
  • 7
Loading