title: Callback function pointers C++ with/without classes
body: <p>I got stuck. I am trying to form a function that will eat classless function pointers and ones from objects. Here is my current code that hopefully explains more.</p>
  
  <p>(It should run on a Arduino, so I cannot use big libraries.)</p>
  
  <p>First off, I am using this library for the Arduino:</p>
  
  <pre><code>/* SimpleTimer - A timer library for Arduino.
   * Author: mromani@ottotecnica.com
   * Copyright (c) 2010 OTTOTECNICA Italy
   */
  </code></pre>
  
  <p>Which takes functions which it calls on a set timer interval of this type:</p>
  
  <pre><code>typedef void (*timer_callback)(void);
  </code></pre>
  
  <p>As far as my knowledge goes, it's a classles function, the webpage <em><a href="http://www.parashift.com/c++-faq-lite/pointers-to-members.html">Pointers to member functions</a></em> got me really far but, not far enough. Probably a terminology deficit on my side.</p>
  
  <p>Now, I have made my own class which I would like in turn to use this SimpleTimer library. But if I feed the SimpleTimer my class functions, it does not like them (what I understand). But how would it be possible to make this happen without altering the SimpleTimer library.</p>
  
  <p>So there is the class Robot, which has <code>Robot::halt()</code>. I want the robot to move forward for a set amount of time. Like so:</p>
  
  <pre><code>void Robot::forward(int speed, long time) {
      reset();
      timer.setTimer(time, c_func, 1);
  
      analogWrite(l_a, speed);
      analogWrite(r_a, speed);
      isMoving(true);
  }
  
  void Robot::halt() {
      __isMoving = false;
      digitalWrite(r_a, LOW);
      digitalWrite(r_b, LOW);
      digitalWrite(l_b, LOW);
      digitalWrite(l_a, LOW);
  }
  </code></pre>
  
  <p>The c_func variable is a classless function at this point, but I would like to use the <code>Robot::halt</code> function. I have looked, read, learned but haven't succeeded yet. I just can't seem to wrap my head around this one because I am missing some angle.</p>
  
  <p>I tried:</p>
  
  <pre><code>timer.setTimer(time, (this-&gt;*halt), 1);
  timer.setTimer(time, Robot::*halt, 1);
  timer.setTimer(time, &amp;Robot::halt), 1);
  </code></pre>
  
  <p>But it would all amount to the same problem/ me just stabbing in the dark here...</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Earlier, I said not wanting to change the SimpleTimer library code. I want to comeback on this one, I guess altering it there would be the better option.</p>
  
  <p>Thanks for all the current answers already, I was only allowed to flag one as a viable answer, actually everyhting I read here was extremely helpful.</p>
  
  <p>To continue this, changing the SimpleTimer code. This class needs to have a reference to the object that holds my "halt" function, right? So, overloading the settimer function to something that takes my object and my function as two seperate pointers would work...? I think I am getting the hang of this but, I am not there yet with my head.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I don't know who came with this one again but, anyone finding this thread. If found <em><a href="http://www.codeproject.com/KB/cpp/FastDelegate.aspx">Member Function Pointers and the Fastest Possible C++ Delegates</a></em> to give a very nice introduction in function pointers and member function pointers.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Got it working, changed the SimpleTimer library to use this Delegate system:
  <a href="http://www.codeproject.com/KB/cpp/FastDelegate.aspx">http://www.codeproject.com/KB/cpp/FastDelegate.aspx</a></p>
  
  <p>It integrated very nicely, and it could be nice to have a standard Delegate system like this in the Arduino library.</p>
  
  <p>Code as in test (working)</p>
  
  <p>typedef</p>
  
  <pre><code>typedef FastDelegate0&lt;&gt; FuncDelegate;
  </code></pre>
  
  <p>Code in robot class:</p>
  
  <pre><code>void Robot::test(){
      FuncDelegate f_delegate;
      f_delegate = MakeDelegate(this, &amp;Robot::halt);
  
      timer.setTimerDelg(1, f_delegate, 1);
  }
  
  void Robot::halt() {
      Serial.println("TEST");
  }
  </code></pre>
  
  <p>Code in SimpleTimer class:</p>
  
  <pre><code>int SimpleTimer::setTimerDelg(long d, FuncDelegate f, int n){
      f();
  }
  </code></pre>
  
  <p>Arduino prints TEST in the console.</p>
  
  <p>Next step putting it in an array, don't see a lot of problems there. Thanks everyone, I can't believe the stuff I learned in two days.</p>
  
  <p>What's that smell? Is that the smell of...? Success!</p>
  
  <p>For the ones interested, the used Delegate system does not amount to memory capacity issues:
  With FastDelegate</p>
  
  <pre><code>AVR Memory Usage
  ----------------
  Device: atmega2560
  
  Program:   17178 bytes (6.6% Full)
  (.text + .data + .bootloader)
  
  Data:       1292 bytes (15.8% Full)
  (.data + .bss + .noinit)
  
  
  Finished building: sizedummy
  </code></pre>
  
  <p>Without FastDelegate:</p>
  
  <pre><code>AVR Memory Usage
  ----------------
  Device: atmega2560
  
  Program:   17030 bytes (6.5% Full)
  (.text + .data + .bootloader)
  
  Data:       1292 bytes (15.8% Full)
  (.data + .bss + .noinit)
  
  
  Finished building: sizedummy
  </code></pre>