446,418 Members | 1,088 Online Need help? Post your question and get tips & solutions from a community of 446,418 IT Pros & Developers. It's quick & easy.

 P: n/a Hi, is there a simple way to use operators overloaded in some class for objects of a derived class? In my example I have a class Matrix that represents 2x2 matrices, and I overloaded operator= and operator+ for this class. Then there is a derived class DiagonalMatrix which represents diagonal 2x2-matrices. Now I would like to use the operators = and + also for the diagonal matrices, but the naive way doesn't work: matrix.cc: In function `int main()': matrix.cc:83: error: no match for 'operator=' in 'D3 = Matrix::operator+(const Matrix&) const((&D2))' matrix.cc:22: error: candidates are: DiagonalMatrix& DiagonalMatrix::operator=(const DiagonalMatrix&) Is there a way to do this without redefining these operators in the derived class? Thanks a lot Steffen Here is the code: class Matrix { public: Matrix(float m11=0, float m12=0, float m21=0, float m22=0); Matrix(const Matrix &m); virtual ~Matrix() {delete[] d;}; Matrix operator+(const Matrix &m) const; Matrix& operator=(const Matrix &m); float &c(int i, int j) const {return d[2*(i-1)+(j-1)];}; private: float *d; }; class DiagonalMatrix : public Matrix { public: DiagonalMatrix() {}; DiagonalMatrix(float d1, float d2) {c(1,1) = d1; c(2,2) = d2;}; }; Matrix::Matrix(float m11, float m12, float m21, float m22) { d = new float[2*2]; c(1,1)=m11; c(1,2)=m12; c(2,1)=m21; c(2,2)=m22; } Matrix::Matrix(const Matrix &m) { d = new float[2*2]; for (int i=0 ; i<2 ; i++) for (int j=0 ; j<2 ; j++) c(i,j) = m.c(i, j); } Matrix Matrix::operator+(const Matrix &q) const { Matrix r; for (int i=1 ; i<=2 ; i++) for (int j=1 ; j<=2 ; j++) r.c(i,j) = c(i,j) += q.c(i,j); return r; } Matrix& Matrix::operator=(const Matrix &q) { for (int i=1 ; i<=2 ; i++) for (int j=1 ; j<=2 ; j++) c(i,j) = q.c(i,j); return *this; } int main() { DiagonalMatrix D1(1,2); DiagonalMatrix D2(3,4); DiagonalMatrix D3; D3 = D1 + D2; // this causes the error return 0; } Dec 9 '05 #1 