title: How to get the function pointer of a class member in a function of the same class?
body: <p>I'm stumped on this.
  I have a class <code>Foo</code> with a function <code>DoTheThing1</code> that takes a pointer to a void function with 0 parameters and calls the function.</p>
  
  <pre><code>class Foo {
  public:
     Foo () {}
  
     void DoTheThing1 (void (*theThing)()) {
        theThing();
     }
  };
  </code></pre>
  
  <p>I have another class <code>Bar</code> that has an instance of <code>Foo</code>.
  Class <code>Bar</code> also has its own function <code>DoTheThing2</code> where it tries in it's construct to pass a pointer of <code>DoTheThing2</code> to <code>Foo's</code> <code>DoTheThing1</code>.</p>
  
  <pre><code>class Bar {
  public:
     Foo* foo = new Foo();
  
     Bar () {
        foo-&gt;DoTheThing1(&amp;Bar::DoTheThing2);
     }
  
     void DoTheThing2 () {
        // Something happens.
     }
  
  };
  </code></pre>
  
  <p>I get this error <code>error C2664:  'void Foo::DoTheThing1(void (__cdecl *)(void))': cannot convert argument 1 from 'void (__cdecl Bar::* )(void)' to 'void (__cdecl *)(void)</code> at the line where the function pointer get's passed in.</p>
  
  <pre><code>Bar () {
        foo-&gt;DoTheThing1(&amp;Bar::DoTheThing2); /// Does not like.
     }
  </code></pre>
  
  <p>I'm unsure of how to work around this. It seems some weird cast is required.</p>
  
  <p>EDIT</p>
  
  <p>Actually, my situation is a little more complicated than just calling a function pointer from a class member within itself. What my code actually does is set the pointer to a variable, then it gets called later.</p>
  
  <pre><code>class Foo {
  public:
  
     void (*m_onEvent) ();
  
     Foo () {}
  
     void SetTheThing (void (*theThing)()) {
        m_onEvent = theThing;
     }
  
     template &lt;typename T&gt;
     void SetTheThing (T&amp;&amp; theThing) {
        m_onEvent = theThing;
     }
  
     void DoTheThing1 () {
        m_onEvent();
     }
  };
  
  class Bar {
  public:
     Foo* foo = new Foo();
  
     Bar () {
        foo-&gt;SetTheThing([this](){ DoTheThing2(); });  // error C2440:  '=': cannot convert from 'T' to 'void (__cdecl *)(void)'
        foo-&gt;SetTheThing(&amp;DoTheThing2);                // '&amp;' illegal operation on bound member function expression.
     }
  
     void DoTheThing2 () {
        std::cout &lt;&lt; "I did the thing." &lt;&lt; std::endl;
     }
  
  };
  
  
  int main () {
  
     Bar* bar = new Bar();
     bar-&gt;foo-&gt;DoTheThing1();
  
  }
  </code></pre>
  
  <p>EDIT</p>
  
  <p>So now I'm trying to hack it using a class template, but I keep getting stopped by this error: <code>Term does not evaluate to a function taking 0 arguments.</code></p>
  
  <p>I'm trying to figureout how my function pointer doesn't evaluate to anything.</p>
  
  <pre><code>template &lt;typename U&gt;
  class Foo {
  public:
  
  
     void (U::*m_theThing) ();
  
     Foo () {}
  
     void SetTheThing (void (U::*theThing)()) {
        m_theThing = theThing;
     }
  
     void DoTheThing1 () {
        m_theThing(); // Term does not evaluate to a function taking 0 arguments.
     }
  };
  
  class Bar {
  public:
     Foo&lt;Bar&gt;* foo = new Foo&lt;Bar&gt;();
  
     Bar () {
        foo-&gt;SetTheThing(&amp;Bar::DoTheThing2);
     }
  
     void DoTheThing2 () {
        std::cout &lt;&lt; "I did the thing." &lt;&lt; std::endl;
     }
  };
  
  
  int main () {
     Bar* bar = new Bar();
     bar-&gt;foo-&gt;DoTheThing1();
  }
  </code></pre>