title: Passing member function pointer to member object in c++
body: <p>I have a problem with using a pointer to function in C++. Here is my example:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  class bar
  {
  public:
      void (*funcP)();
  };
  
  class foo
  {
  public:
      bar myBar;
      void hello(){cout &lt;&lt; "hello" &lt;&lt; endl;};
  };
  
  void byebye()
  {
      cout &lt;&lt; "bye" &lt;&lt; endl;
  }
  
  
  int main()
  {
      foo testFoo;
  
      testFoo.myBar.funcP = &amp;byebye;         //OK
      testFoo.myBar.funcP = &amp;testFoo.hello;  //ERROR
      return 0;
  }
  </code></pre>
  
  <p>Compilator returns an error at <code>testFoo.myBar.funcP = &amp;testFoo.hello;</code>:</p>
  
  <blockquote>
    <p><strong>ISO C++ forbids taking the address of a bound member function to form a
    pointer to member function.  Say
    '&amp;foo::hello'</strong></p>
    
    <p><strong>cannot convert 'void (foo::<em>)()' to 'void (</em>)()' in assignment</strong></p>
  </blockquote>
  
  <p>So i tried it like this:</p>
  
  <pre><code>class bar
  {
  public:
      void (*foo::funcP)();
  };
  </code></pre>
  
  <p>But now the compilator adds one more:</p>
  
  <blockquote>
    <p><strong>'foo' has not been declared</strong></p>
  </blockquote>
  
  <p>Is there a way make it work?</p>
  
  <p>Thanks in advance for suggestions</p>