title: Having pointer to a template base class
body: <p>In the following code, I've got an interface called <code>IDecoder</code> which should be implemented by any decoder class (<code>DecoderA</code> and <code>DecoderB</code> here). <code>IDecoder</code> has a <code>bitset&lt;&gt;</code> in it, therefore I've made <code>IDecoder</code> a template class and the size of <code>bitset</code> is determined in the derived classes. I've also got a <code>Container</code> class which should hold a pointer to the <code>IDecoder</code> class, but since <code>IDecoder</code> is not a complete type, I've added a dummy <code>DecoderBase</code> class from which <code>IDecoder</code> inherits:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;bitset&gt;
  
  class DecoderBase
  {
  public:
      DecoderBase(){}
      virtual ~DecoderBase(){}
  };
  
  template&lt;size_t nb&gt;
  class IDecoder : public DecoderBase
  {
  public:
      IDecoder(){}
      virtual ~IDecoder(){}
      virtual std::bitset&lt;nb&gt; GetDecodedFields() = 0;
  };
  
  const int DecoderAMaxFields = 100;
  const int DecoderBMaxFields = 200;
  
  class DecoderA : public IDecoder&lt;DecoderAMaxFields&gt;
  {
  public:
      DecoderA(){}
      ~DecoderA(){}
      std::bitset&lt;DecoderAMaxFields&gt; GetDecodedFields()
      {
          std::bitset&lt;DecoderAMaxFields&gt; bits;
          // assume we've decoded fileds number 1 and 10 here
          bits[1] = 1;
          bits[10] = 1;
          return bits;
      }
  };
  
  class DecoderB : public IDecoder&lt;DecoderBMaxFields&gt;
  {
  public:
      DecoderB(){}
      ~DecoderB(){}
      std::bitset&lt;DecoderBMaxFields&gt; GetDecodedFields()
      {
          std::bitset&lt;DecoderBMaxFields&gt; bits;
          // assume we've decoded fileds number 11, 29, 110 &amp; 142 here
          bits[11] = 1;
          bits[29] = 1;
          bits[110] = 1;
          bits[142] = 1;
          return bits;
      }
  };
  
  class Container
  {
  public:
      Container(){}
      virtual ~Container(){}
      void SetDecoder(DecoderBase* decoder)
      {
          mDecoder = decoder;
      }
      DecoderBase* GetDecoder()
      {
          return mDecoder;
      }
  private:
      DecoderBase* mDecoder;
  };
  
  int main()
  {
      Container container;
      container.SetDecoder(new DecoderA());
      ((DecoderA*)container.GetDecoder())-&gt;GetDecodedFields();
      return 0;
  }
  </code></pre>
  
  <p>As you can see, before calling <code>GetDecodedFields()</code> I have to cast it to one of the decoders, which is the exact reverse opposite of the purpose of polymorphism! How can I fix this code? I've also thought of using <code>boost::dynamic_bitset</code> but I'd prefer not to use it for it's slower than normal <code>bitset</code>.</p>