title: Deriving from a class with operator overloading
body: <p>I want to create a collection of classes that behave like math vectors, so that multiplying an object by a scalar multiplies each field by that ammount, etc. The thing is that I want the fields to have actual names, instead of being treated as an index. </p>
  
  <p>My original idea to implement this was creating a base class Rn with the overloads and then create derived classes with the pretty names. Something like this:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;algorithm&gt;
  using namespace std;
  
  template&lt;int N, class X=double&gt;
  struct Base{
      X xs[N];
  
      Base(){};
  
      Base(X *data){
          copy(data, data+N, xs);
      }
  
      Base operator*= (double d){
          for(int i=0; i&lt;N; i++){
              xs[i] *= d;
          }
          return *this;
      }
  
      Base operator* (double d){
          Base answer = *this;
          answer *= d;
          return answer;
      }
  
      //also operators for +=, +, multiplication from left, maybe [] too
  };
  
  struct Derived : public Base&lt;2&gt;{
      Derived(double a, double b){
          foo() = a;
          bar() = b;
      }
  
      double &amp;foo(){ return xs[0]; }
      double &amp;bar(){ return xs[1]; }
  };
  
  int main()
  {
      //this is OK:
      double data[2] = {0.0, 2.0};
      Base&lt;2&gt; b(data);
      b = b*17.0;
  
      cout &lt;&lt; b.xs[0] &lt;&lt; endl;
  
      //I can't do this:
      Derived x(0.0, 2.0);
      x = x*17.0;
  
      cout &lt;&lt; x.foo() &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>
  
  <p>I get a compiler error whenever I try to use of of the operators that requires copying.
  gcc gave me the following compiler error:</p>
  
  <pre><code>teste.cpp: In function ‘int main()’:
  teste.cpp:52: error: no match for ‘operator=’ in ‘x = x.Derived::&lt;anonymous&gt;.Base&lt;N, X&gt;::operator* [with int N = 2, X = double](1.7e+1)’
  teste.cpp:31: note: candidates are: Derived&amp; Derived::operator=(const Derived&amp;)
  </code></pre>
  
  <p>I think the problem is that the overloading functions deal with Base objects that can't be converted to Derived ones, so I can't use them in the derived class. However, I can't come up with a solution. Is there a way around this or should I use a totally different approach?</p>
  
  <p>Bonus question: is there some way that I can use std::valarray to keep from having to type lots and lots of operator overloads?</p>