title: C++ Building an API using Interface classes, Templates and Object Factories
body: <p>I'd like to build an API that provides a class interface to functionality (header file) and hide the implementation for all the standard good reasons.  I plan on using an object factory to return object pointers of derived "newed" objects which conform to the interface.</p>
  
  <p>My core API class varies based on std::vectors of built-in numeric types(char, uchar, short, ushort, int, uint, float and double).  So a template seems like a natural fit.  I'll make an interface class template available to the users of my API and derive from it in the implementation class template which will be hidden.</p>
  
  <p>Since my user visible class template is an interface class I'd like to declare all the methods pure virtual but I understand there may problems with doing this given required template instantiations/exports in dlls, shared objects, etc.  So I'll just define them virtual and give them empty method bodies in the base interface class.  Somewhere along the line I need to create a template static factory method (or template function) that will create the objects of the derived classes and return pointers to them to the caller.</p>
  
  <p>The issue is I cannot put the implementation of the static object factory method in the interface header file either as it has to create objects of the derived classes which are to be hidden. So I'd like to put these static object factories in the implementation header or a source file.</p>
  
  <p>Here is a conceptual implementation header</p>
  
  <pre><code>#ifndef INTERFACE_H
  #define INTERFACE_H
  
  #ifdef DLL_EXPORTS
  #define DLL_API __declspec(dllexport)
  #else
  #define DLL_API __declspec(dllimport)
  #endif
  
  // This interface class is exported from the dll.
  
  template &lt; typename T &gt;
  class DLL_API InterfaceClass {
  public:
   static InterfaceClass* factoryMethod( );
   virtual ~InterfaceClass ( ) { }
  
   virtual void someMethod( T aParam ){ };
  
  protected:
   InterfaceClass ( ) { }
  
  private:
   InterfaceClass ( const InterfaceClass &amp; );
   InterfaceClass&amp; operator=( const InterfaceClass &amp; );
  };
  
  #endif
  </code></pre>
  
  <p>Here is a conceptual derived implementation class</p>
  
  <pre><code>#ifndef IMPLEMENTATION_H
  #define IMPLEMENTATION_H
  
  #include &lt;vector&gt;
  #include "interface.h"
  
  template &lt; typename T &gt;
  class DerivedClass : public InterfaceClass&lt; T &gt; {
  public:
   DerivedClass( const T&amp; aDataVector ) : InterfaceClass&lt; T &gt;( ) { /*...*/ }
  
   virtual ~DerivedClass( ) { /*...*/ }
  
   virtual void someMethod( T aParam ) { /*...*/ }
  private:
   std::vector&lt; T &gt; _dataVector;
  </code></pre>
  
  <p>};</p>
  
  <p>Note: In reality I'll be using TR1::shared_ptr instead of raw pointers.</p>
  
  <p>My questions are:</p>
  
  <p>1) Where do I define the static "factoryMethod( )" method (implementation.h or implementation.cpp)?</p>
  
  <p>2) What does this method implementation look like? </p>
  
  <p>3) Are there any other issues I need to be aware of so I or my API users don't get link time or run-time errors?</p>
  
  <p>thanks in advance!</p>