title: dynamic array member variables
body: <p>Hey i'm new to c++ and still working out its perticularities.  I'm having the darnedest time trying to figure out whats going wrong with this code.  I've stepped through it and everything is calculating correctly.  The issue is that value_array in the base class doesn't seem to be retaining the values once the derived class Calculate function ends.  I think i've declared and allocated the array properly.  I'm stumped...</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  class Indicator 
  {
  protected:
      double * value_array;
      double * input_array;
      int input_size;
  public:
      Indicator(double input[], int size) 
      { 
          input_array = input; 
          input_size = size; 
          value_array = new double[size]; // issue with value_array
      }
      double operator[] (int index) { return value_array[index]; }
      void virtual Calculate() {}
      ~Indicator() { delete[] value_array; }
  };
  
  class SMA : public Indicator 
  {
  private:
      int nperiod;
      double sum;
  public:
      SMA(double input[], int size, int period) : Indicator(input, size) 
      { 
          nperiod = period; 
          sum = 0; 
          Calculate(); 
      }
      void Calculate();
  };
  
  void SMA::Calculate()
  { 
      for (int i=0; i&lt;input_size; i++)
      {
          if (i &gt; nperiod - 1) 
          {
              sum += input_array[i] - input_array[i-nperiod];
              value_array[i] = sum / nperiod;
          }
          else
          {
              sum += input_array[i];
              value_array[i] = sum / (i+1);
          }
      }
  }
  
  int main(int argc, const char *argv[]) {
      double input[] = {1,2,3,4,5,6,7,8,9,10};
      Indicator indicator = SMA(input,10,5);
  
      double value = indicator[0];
      std::cout &lt;&lt; "value: " &lt;&lt; value &lt;&lt; std::endl;
  
      std::cin.get();
      exit(0);
  }
  </code></pre>
  
  <h3>Update:</h3>
  
  <p>Here is the code implemented with vectors.  I wanted to leave the input as double[] to be consistent with other libraries, any other potential issues I should be aware of?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  class Indicator 
  {
  protected:
      std::vector&lt;double&gt; value_vector;
      double * input_array;
      int input_size;
  public:
      Indicator(double input[], int size) 
      { 
          input_array = input; 
          input_size = size; 
          value_vector.reserve(size);
      }
      double operator[] (int index) { return value_vector[index]; }
      void virtual Calculate() {}
  };
  
  class SMA : public Indicator 
  {
  private:
      int nperiod;
      double sum;
  public:
      SMA(double input[], int size, int period) : Indicator(input, size) 
      { 
          nperiod = period; 
          sum = 0; 
          Calculate(); 
      }
      void Calculate();
  };
  
  void SMA::Calculate()
  { 
      for (int i=0; i&lt;input_size; i++)
      {
          if (i &gt; nperiod - 1) 
          {
              sum += input_array[i] - input_array[i-nperiod];
              value_vector.push_back(sum / nperiod);
          }
          else
          {
              sum += input_array[i];
              value_vector.push_back(sum / (i+1));
          }
          std::cout &lt;&lt; "sma:  " &lt;&lt; value_vector[i] &lt;&lt; std::endl;
      }
  }
  
  int main(int argc, const char *argv[]) {
      double input[] = {1,2,3,4,5,6,7,8,9,10};
      Indicator indicator = SMA(input,10,5);
  
      for (int i=0; i&lt;10; i++)
      {
          std::cout &lt;&lt; "main: " &lt;&lt; indicator[i] &lt;&lt; std::endl;
      }
  
      std::cin.get();
      exit(0);
  }
  </code></pre>