I want to create a collection of classes that behave like mathematical vectors, so multiplying an object by a scalar multiplies each field by that number, etc. The thing is, I want the fields to have actual names, and not be treated as an index.
My initial idea to implement this is to create an Rn base class with overloads, and then create derived classes with nice names. Something like that:
#include <iostream> #include <algorithm> using namespace std; template<int N, class X=double> struct Base{ X xs[N]; Base(){}; Base(X *data){ copy(data, data+N, xs); } Base operator*= (double d){ for(int i=0; i<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<2>{ Derived(double a, double b){ foo() = a; bar() = b; } double &foo(){ return xs[0]; } double &bar(){ return xs[1]; } }; int main() { //this is OK: double data[2] = {0.0, 2.0}; Base<2> b(data); b = b*17.0; cout << b.xs[0] << endl; //I can't do this: Derived x(0.0, 2.0); x = x*17.0; cout << x.foo() << endl; return 0; }
I get a compiler error whenever I try to use statements that require copying. gcc gave me the following compiler error:
teste.cpp: In function 'int main()': teste.cpp:52: error: no match for 'operator=' in 'x = x.Derived::<anonymous>.Base<N, X>::operator* [with int N = 2, X = double](1.7e+1)' teste.cpp:31: note: candidates are: Derived& Derived::operator=(const Derived&)
I think the problem is that overload functions deal with basic objects that cannot be converted to derivatives, so I cannot use them in a derived class. However, I cannot find a solution. Is there a way around this or should I use a completely different approach?
Bonus question: is there a way I can use std :: valarray so as not to print many, many operator overloads?
source share