Inheriting methods so that child classes can communicate

I have a question:

I've created a Linear algebra Matrix class which supports matrix multiplication with:
1
2
Matrix Matrix::operator* (Matrix& rhs); // I don't think this can be inherited
Matrix Matrix::multiply (Matrix& rhs);  // operator* just calls this which is inherited 


Other classes that inherit Matrix will be things like IdentityMatrix and Vector which are just very specific cases of a Matrix. Vector includes some additional methods such as CrossProduct and DotProduct, but all private members are inherited.
Example:
1
2
3
4
5
6
7
class Vector : public Matrix
{
public: 
  Vector(int rows) : m_rows(rows), m_cols(0) {}
  Vector crossProduct(Vector& v);
  Vector dotProduct(Vector& v);
};


Now if I want to multiply an identity matrix by a vector, I would do this:
1
2
3
4
5
6
Vector v(/*...*/);
IdentityMatrix I(/*...*/);

std::cout << I*v << std::end;
// or
std::cout << I.multiply(v) << std::endl;


I get errors that no function matches the argument list or no operator* matches these operands. I understand why, but is there a way around it?

It doesn't make sense to redefine EVERY operation for every type of child class, especially when one child shouldn't need to know about the existence of the others. I just want them to treat every other child like its parent: Matrix.
Last edited on
I found a solution to this:

Whenever I define a child class, I need to define a conversion:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template <class T>
class Vector : public Matrix<T>
{
public:
  Vector(int rows)
    : Matrix( rows, 0 )
  {  }

  operator Matrix()
  {
    Matrix retobj(m_rows, m_cols);
    for (int i = 0; i < m_rows; ++i)
      for (int j = 0; j < m_cols; ++j)
        retobj.m_data[i * m_cols + j] = m_data[i * m_cols + j);
    return retobj;
  }
};


The downside is that I am doing lots of creations here, one inside the operator Matrix() and one is a copy constructor once the return value actually gets received in the calling function. I'm not sure if this is the best way to do it.
Last edited on
While I can understand the temptation to define a Vector class, I can't really see it doing much beyond simplifying the creation of a Matrix which corresponds to a Vector (and complicating everything else,) and you could do that using a simple function:

Matrix RowVector(int rows) { return Matrix(rows,0); }

[edit: A vector isn't necessary rowsx1, it could be 1xcolumns.]

dotproduct and crossproduct don't really make much sense in a Vector, IIRC. On the other hand, they'd make a lot of sense in a Matrix.

The variables called rhs in the first snippet of code in the OP should be const.
Last edited on
Hmmm I see your point, but I do think the cross product makes more sense on just a vector. The wikipedia article on cross product defines it as a "operation on two vectors in three-dimensional space". I could implement it as an outerproduct though in a matrix form.

Regarding your idea to simplify the creation of a matrix with a function... is there a way to do this with typedef or #define ?

I could use the following for new objects, but object defintions are not any easier:
#define RowVector(rows) (Matrix(rows,0))

Something like the following would be perfect!
typedef Matrix(rows,0) RowVector(rows);
Last edited on
Topic archived. No new replies allowed.