title: Passing a member function as a callback, boost::bind, boost::function
body: <p>I stumbled upon a curiosity, when investigating the possibility of passing member functions as callbacks using boost::bind and boost::function. I was fooling around with a model of two classes. The first one (Organism) exposes its member variable (age) through an int(void) function (getAge). The second class (Biologist) stores a boost::function as a member (callbackFunction) and uses it to determine (takeNotes) the current age of the animal it is studying (it retains the age in the member variable m_notes). An instance (steve_irwin) of the second class is supposed to 'watch' (takeNotes) the instance (animal) of the first class. </p>
  
  <p>Here is the code implementing the Animal class:</p>
  
  <pre><code>class Organism {
  public:
      Organism(int = 1);
      void growOlder();
      int getAge(void);
      void tellAge(void);
  private:
      int m_age;
  };
  
  Organism::Organism(int _age) : m_age(_age) {}
  
  void Organism::growOlder() {
      m_age++;
  }
  
  int Organism::getAge(void) {
      return m_age;
  }
  
  void Organism::tellAge(void) {
      std::cout &lt;&lt; "This animal is : " &lt;&lt; m_age &lt;&lt; " years old!";
  }
  </code></pre>
  
  <p>Whereas this is the code implementing the Biologist class:</p>
  
  <pre><code>class Biologist {
  public:
      void setCallback(boost::function&lt;int(void)&gt;);
      void takeNotes();
      void tellAge();
  private:
      boost::function&lt;int(void)&gt; updateCallback;
      int m_notes;
  };
  
  void Biologist::setCallback(boost::function&lt;int(void)&gt; _f) {
      updateCallback = _f;
  }
  
  void Biologist::takeNotes() {
      m_notes = updateCallback();
  }
  
  void Biologist::tellAge() {
      std::cout &lt;&lt; "The animal I am studying is : " &lt;&lt; m_notes &lt;&lt;
          " years old! WOW!" &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>The main loop goes like this:</p>
  
  <pre><code>Organism animal(3);
  Biologist steve_irwin;
  
  boost::function&lt;int(void)&gt; f = boost::bind(&amp;Organism::getAge, animal);
  steve_irwin.setCallback(f);
  
  steve_irwin.takeNotes();
  steve_irwin.tellAge();
  animal.tellAge();
  
  animal.growOlder();
  
  steve_irwin.takeNotes();
  steve_irwin.tellAge();
  animal.tellAge();
  </code></pre>
  
  <p>I create an animal which is 3 years old, I tell Steve Irwin to watch it, he tells its age correctly after taking notes at first, but after the animal grows older and he tells its age again, he still thinks the animal is 3 years old.</p>
  
  <p>The output of the program :</p>
  
  <pre><code>The animal I am studying is : 3 years old! WOW!
  This animal is : 3 years old!
  The animal I am studying is : 3 years old! WOW!
  This animal is : 4 years old!
  </code></pre>
  
  <p>I am guessing I somehow failed to pass the member function as a callback by reference but I can't determine where. Can you help me?</p>