title: Dynamic Libraries, plugin frameworks, and function pointer casting in c++
body: <p>I am trying to create a very open plugin framework in c++, and it seems to me that I have come up with a way to do so, but a nagging thought keeps telling me that there is something very, very wrong with what I am doing, and it either won't work or it will cause problems.</p>
  
  <p>The design I have for my framework consists of a Kernel that calls each plugin's <code>init</code> function. The init function then turns around and uses the Kernel's <code>registerPlugin</code> and <code>registerFunction</code> to get a unique id and then register each function the plugin wants to be accessible using that id, respectively.</p>
  
  <p>The function registerPlugin returns the unique id. The function registerFunction takes that id, the function name, and a generic function pointer, like so:</p>
  
  <pre><code>bool registerFunction(int plugin_id, string function_name, plugin_function func){}
  </code></pre>
  
  <p>where plugin_function is</p>
  
  <pre><code>typedef void (*plugin_function)();
  </code></pre>
  
  <p>The kernel then takes the function pointer and puts it in a map with the <code>function_name</code> and <code>plugin_id</code>. All plugins registering their function must caste the function to type <code>plugin_function</code>.</p>
  
  <p>In order to retrieve the function, a different plugin calls the Kernel's</p>
  
  <pre><code>plugin_function getFunction(string plugin_name, string function_name);
  </code></pre>
  
  <p>Then that plugin must cast the <code>plugin_function</code> to its original type so it can be used. It knows (in theory) what the correct type is by having access to a <code>.h</code> file outlining all the functions the plugin makes available. Plugins, by the by, are implemented as dynamic libraries.</p>
  
  <p>Is this a smart way to accomplish the task of allowing different plugins to connect with each other? Or is this a crazy and really terrible programming technique? If it s, please point me in the direction of the correct way to accomplish this.</p>
  
  <p>EDIT: If any clarification is needed, ask and it will be provided.</p>