473,385 Members | 1,392 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Operator overloading and inheritance

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
2 3028

Steffen wrote:


int main()
{
DiagonalMatrix D1(1,2);
DiagonalMatrix D2(3,4);
DiagonalMatrix D3;

D3 = D1 + D2; // this causes the error

The return value of operator+ is a _value_ , not a reference or
pointer. So there is no legal conversion from the derived class _value_
to base class _value_. Hence the error.

return 0;
}


Dec 9 '05 #2
Steffen wrote:
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?


It's generally a BAD IDEA(tm). First of all, your 'DiagonalMatrix' is not
necessarily properly derived. Think of this: a diagonal matrix has some
specific properties not all matrices have, right? Now, if I create some
algorithm that works on a "plain", unconstrained, Matrix, and then pass
a DiagonalMatrix to it, the algorithm can change the contents of my object
without concerning itself with the properties a diagonal matrix has, no?
It can simply add something off the main diagonal, and here you go, your
DiagonalMatrix object is not true any more.

So, whatever operation is performed by a member function of the base class
is not necessarily the right operation for the derived class, and that's
why you should think twice before allowing that.

But generally speaking, if you want to allow your operator+ that you wrote
for 'Matrix' to be used on a 'DiagonalMatrix', you will have to live with
the fact that it returns not a DiagonalMatrix, but only a Matrix. To make
it compile, define operator+ as NON-member. If you need it to be able to
access private data of Matrix objects, declare it a friend. That will
allow conversions (from DiagonalMatrix& to Matrix&) to be performed on
both operands, and not only on the right one.

V
Dec 9 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Fuzzyman | last post by:
I've been programming in python for a few months now - and returning to programming after a gap of about ten years I've really enjoyed learning python. I've just made my first forays into...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
2
by: cppsks | last post by:
Here is the code that I am working with: #include <new> #include <iostream> #include "stdlib.h" class Base1 { public: Base1() { cout << "Base1:" << this << endl; }
0
by: cppsks | last post by:
Hello. I posted a question regarding this yesterday. I came up with the following solution but I am a little hesistant as to this solution having any side-effects that I am not aware of. The...
5
by: James Angi | last post by:
I have a question on operator overloading that I can't find an answer to in several guides (even google has failed me). I'm currently making my way through several C++ guides, trying to become...
19
by: jacob navia | last post by:
C++ introduced an interesting feature (among others): operator overloading. The idea is to build a mechanism for the user defining its own number types and the operations to be done with them. ...
6
by: apm | last post by:
Recently I have had to use a value type for a complex structure because I don't know how to override the = operator. Can the operator ever be overloaded? Or can inheritance be used with value types?
6
by: Massimo Soricetti | last post by:
Hello, recently I wrote a little class which has to wrap two different type of data, showing the same external interface. I used operator overloading, but the same result I could eventually...
0
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones example. What I am doing is defining the...
11
by: dascandy | last post by:
Hello, I was wondering, why is overloading operator. (period) forbidden? It would make a few odd applications possible (dynamic inheritance and transparent remote method invocation spring to my...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.