title: How to write a C++ API class that will allow a user to register its own callback functions?
body: <p>Let's say a user links his app against a library I wrote and I want to let him specify a callback function that I will call whenever an error occurs in my library. The implementation below works but I want to double check that I'm not missing something here:</p>
  <ul>
  <li>Thread safety</li>
  <li>DLL initialization issues</li>
  <li>Public API considerations (I'm giving away a reference to an instance from the DLL is that OK?)</li>
  <li>Anything that could be done better to hide implementation details from the public API?</li>
  </ul>
  <p><strong>errordispatcher.h</strong>:</p>
  <pre class="lang-cpp prettyprint-override"><code>#pragma once
  
  #include &lt;functional&gt;
  #include &lt;memory&gt;
  #include &lt;string&gt;
  
  namespace WE
  {
  class ErrorDispatcher 
      {
      public:
      
          ErrorDispatcher()
          {}
  
          explicit ErrorDispatcher(std::function&lt;void(std::string)&gt; user_func)
          : error_callback_func{user_func}
          {}
          
          virtual ~ErrorDispatcher(){}
          
          static ErrorDispatcher&amp; getInstance()
          {
              return instance_;
          }
          
          void setErrorCallback(std::function&lt;void(std::string)&gt; user_func)
          {
              error_callback_func = nullptr;
              if (user_func)
                  error_callback_func = user_func;
          }
          
          void dispatchError(std::string message)
          {
              if (error_callback_func)
                  error_callback_func(message);
          }
          
      private:
      
          explicit ErrorDispatcher(const ErrorDispatcher&amp;) = delete;
          explicit ErrorDispatcher(ErrorDispatcher&amp;&amp;) = delete;
          ErrorDispatcher&amp; operator = (const ErrorDispatcher&amp;) = delete;
          ErrorDispatcher&amp; operator = (ErrorDispatcher&amp;&amp;) = delete;
  
          static ErrorDispatcher instance_;
          std::function&lt;void(std::string)&gt; error_callback_func = nullptr;
      };
  }
  </code></pre>
  <p><em>NOTE: above I have inline implementation details in the public header to make this post shorter but they will be moved to a .cpp and won't be part of the public header</em></p>
  <p><strong>errordispatcher.cpp</strong>:</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &quot;errordispatcher.h&quot;
  
  namespace WE
  {
      ErrorDispatcher ErrorDispatcher::instance_;
  }
  </code></pre>
  <p><strong>apitest.h</strong></p>
  <pre class="lang-cpp prettyprint-override"><code>namespace WE
  {
      void dllFunctionThatMightGiveError();
  }
  </code></pre>
  <p><strong>apitest.cpp</strong></p>
  <pre class="lang-cpp prettyprint-override"><code>#include &quot;errordispatcher.h&quot;
  #include &quot;apitest.h&quot;
  
  namespace WE
  {
      void dllFunctionThatMightGiveError()
      {
          // Some error happens in dll so call user function and give a message to the user!
          ErrorDispatcher::getInstance().dispatchError(&quot;Error in DLL!&quot;);
      }
  }
  </code></pre>
  <p><strong>main.cpp</strong> (<em><strong>USER APP</strong></em>)</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &quot;errordispatcher.h&quot;
  #include &quot;apitest.h&quot;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  void error_callback(std::string message)
  {
      std::cout &lt;&lt; message &lt;&lt; &quot;\n&quot;;
  }
  
  int main(void)
  {
      WE::ErrorDispatcher::getInstance().setErrorCallback(error_callback);
      WE::ErrorDispatcher::getInstance().dispatchError(&quot;Error in APP!&quot;);
      WE::dllFunctionThatMightGiveError();
      return 0;
  }
  </code></pre>
  <p><strong>Output is</strong>:</p>
  <blockquote>
  <p>Error in APP!</p>
  <p>Error in DLL!</p>
  </blockquote>