Hey all, here is the problem:
I'm built a Matrix class and I did the dirty work redefining all the boring operators (+,-,* etc.). Now I needed a specifical Vector class, so I decided to let it inherit from Matrix class (as vectors, for the most, should act like n x 1 or 1 x n matrices). So i have, for example, a Matrix operator+ function working this way
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
VMatrix VMatrix::operator+ (const VMatrix &add)
{
if (rows() == add.rows() && cols() == add.cols())
{
VMatrix r(rows(),cols());
for(int i=0;i<rows();i++)
{
for (int j=0;j<cols();j++)
{
r[i][j] = m[i][j] + add[i][j];
}
}
return r;
}
cout << "Bad operation."<<endl;
return VMatrix();
}
|
it obviously does the sum of the two matrices and returns a Matrix element. Now if my Vector class inherits this method is awesome but there is a problem: the sum of two vectors will return a Matrix not a Vector!
This makes lines of code like this
(v1+v2).inline_print() //function implemented *just* in Vector class
uncompilable. And i saw some strange errors like, for example, vectors divided by some constant becoming 0. Is there a clean way to let the Vector class inherit the same operators but with function returning the correct class type? Redefining all operators would make inheriting useless.
Thanks in advance, valleyman