title: C++ Templates polymorphism obstacle
body: <p>Interface:</p>
  
  <pre><code>template &lt;class T&gt;
  class Interface{
      public:
      typedef T Units;
      virtual T get() = 0;
  };
  </code></pre>
  
  <p>Implementation1:</p>
  
  <pre><code>class Implementation1: public Interface&lt;float&gt; {
      public:
  
      float get() {
         return 0.0f;
      }
  
  };
  </code></pre>
  
  <p>Implementation2:</p>
  
  <pre><code>class Implementation2: public Interface&lt;int&gt; {
      public:
  
      int get() {
         return 0;
      }
  
  };
  </code></pre>
  
  <p>Container (with errors):</p>
  
  <pre><code>class Container{
      private:
      Interface* floatGetter;
      int n;
      Timer::Units* array;
  
      public:
      Container(Interface* floatGetter, int n) {
          this-&gt;floatGetter= floatGetter;
          this-&gt;n = n;
          array = new Timer::Units[n];
      }
  
      ~Container() {
  
      }
  
  };
  </code></pre>
  
  <p>For more details, I have a template interface and a derived class from this interface without template. Some other class take an object of the derived class but it takes the object as an interface (in other words, dependency injection). But the type of the interface in this class is defined by the interface implementation. How to implement this idea in C++?</p>
  
  <p><strong>Edit1:</strong></p>
  
  <p>Example:</p>
  
  <pre><code>Interface&lt;float&gt; myInterface1 = new Implementation1();
  Interface&lt;int&gt; myInterface2 = new Implementation2();
  Container container1 = new Container(myInterface1, 10);
  Container container2 = new Container(myInterface2, 10);
  </code></pre>
  
  <p>I need that container understands interface template argument from its implementation.</p>