title: How can I pass a function by reference for a report callback in C++/CLI?
body: <p>I have some code which handles data files and reports an error when it runs into trouble, but I'm having trouble working out how to give my class a callback function. Here's a quick example of the sort of thing I'm trying to achieve:</p>
  
  <pre><code>public delegate void Reporter( System::String^ stringToReport );
  
  /// &lt;summary&gt;
  /// Simple file handler with callback options on error
  /// &lt;/summary&gt;
  public ref class MyFileHandler
  {
  private:
      Reporter^ m_reporter;
  
      void ReportError(String^ error)
      {
          if( m_reporter )
          {
              m_reporter( error );
          }
      }
  
  public:
      MyFileHandler()
      {
  
      }
  
      void SetErrorReporter( Reporter^ reporter )
      {
          m_reporter = reporter;
      }
  
      bool FailingOperation()
      {
          return false;
      }
  
      bool GetData()
      {
          bool succeeded = false;
  
          // Do some operation that fails
          succeeded = FailingOperation();
  
          if( succeeded == false )
          {
              ReportError( "Gah, something bad happened!" );
          }
  
      }
  };
  
  
  public ref class MyFileLoader
  {
  private:
      MyFileHandler m_mfh;
  
      void ErrorHandler(String^ errorMsg)
      {
          System::Windows::Forms::MessageBox::Show( errorMsg );
      }
  
  public:
      MyFileLoader()
      {
          m_mfh.SetErrorReporter( &amp;CallbackFunctionTests::MyFileLoader::ErrorHandler );
      }
  };
  </code></pre>
  
  <p>...which has a problem:</p>
  
  <pre><code>1&gt;CallbackTest.h(131) : error C3374: can't take address of 'CallbackFunctionTests::MyFileLoader::ErrorHandler' unless creating delegate instance
  </code></pre>
  
  <p>So I get the impression I'm mis-using or misunderstanding things. Maybe there's a better way of achieving this?</p>