# operator overloaeded inheritance

• 01-11-2006
dude543
operator overloaeded inheritance
Every book says that if you overload an operator(but =) in base class , you could use it in any derived class.

How do I use the point::operator+(point) with objects of type point1 ?

Code:

```#include <cstdlib> #include <iostream> using namespace std; class point {     int  x,y; public:     point(void)       {       }     point(int Ix, int Iy)       {       x = Ix;       y = Iy;       }     void show(void) const       {       cout << "x = " << x << " y = " << y << endl;       }     point operator+(point p)       {       point  ret;       ret.x = x + p.x;       ret.y = y + p.y;       return(ret);       } }; class point1 : public point     { public:     point1(void):point()         {         }     point1(int Ix, int Iy):point(Ix,Iy)         {         }     point1(point &pin)         {         (*this) = (point1)pin;         }     point1 operator+(point1 p)       {         point pcur = (point)(*this);         point pon = (point)p;         point pans = pcur+pon;         // Until this line everthing is good         // then he says         // "no matching function for call to `point1::point1(point&)'         //         // so I tried point1::point1(point &pin)         // and everthing crashes !!!         return( (point1)(pans));       } }; int main(void) { point1  p1(2,3); point1  p2(18, 47); point1  p3; p3 = p1+p2; p3.show(); system("PAUSE"); return EXIT_SUCCESS; }```
• 01-12-2006
grumpy
If I'm understanding you right, you want to be able to call point::operator+() from within point1::operator+(). The way to do it would be (using your declarations);
Code:

```point1 point1::operator+(point1 p) {     point1 temp;     temp.point::operator+((point)p);     //  other stuff specific to point1 addition     return temp; }```
I also suggest eliminating the constructor of point1 that accepts a point as an argument.

Incidentally, I suggest you would often be better off passing arguments of type point or point1 via const reference to operator+(), and to make the operators const, viz;

Code:

```class point {     // other stuff eliminated for brevity sake     public:     point operator+(const point &p) const;       {       point  ret;       ret.x = x + p.x;       ret.y = y + p.y;       return(ret);       } }; class point1 : public point     { public:     point1 operator+(const point1 &p) const       {             point1 pcur(*this);             pcur.point::operator+(p);  // or pcur.point::operator+((const point &)p);               // other stuff specific to point1 addition             return pcur;       } };```
One advantage of this is that a reference to a derived class can be implicity converted to a reference to an (unambiguous) base, meaning you don't need to do the explicit casting or worry about temporaries that the compiler might introduce.
• 01-12-2006
dude543
Yes, but not What I wanted.
Quote:

Originally Posted by grumpy
Code:

```class point1 : public point     { public:     point1 operator+(const point1 &p) const       {             point1 pcur(*this);             pcur.point::operator+(p);  // or pcur.point::operator+((const point &)p);               // other stuff specific to point1 addition             return pcur;       } };```

I wanted for point1::operator+ to behave just like point::operator+.
This is not what your point1::operator does, because you return pcur, which operate on the +.
For example :
Code:

```point1  p1(2,3), p2(8,7), p3; p3 = p1+p2;```
I would like p3.x = 10 and p3.y = 10.
Your code does p3.x = 2 and p3.y = 3 which is p1 !
• 01-12-2006
Darryl
The problem is with your constructor point1(point).... the point of making the constructor is to tell the compiler how to construct/convert a base object to a derived, and you try to tell it to do it by casting creating an endless loops because it will use the constructor since no cast operator is defined

I corrected the constructor and removed your derived operator+ and it works now as expected
Code:

```#include <cstdlib> #include <iostream> using namespace std; class point {     int  x,y; public:     point(void)       {       }     point(int Ix, int Iy)       {       x = Ix;       y = Iy;       }     void show(void) const       {       cout << "x = " << x << " y = " << y << endl;       }     point operator+(point p)       {       point  ret;       ret.x = x + p.x;       ret.y = y + p.y;       return(ret);       } }; class point1 : public point     { public:     point1(void):point()         {         }     point1(int Ix, int Iy):point(Ix,Iy)         {         }         point1(point &pin):point(pin)         {         }     }; int main(void) { point1  p1(2,3); point1  p2(18, 47); point1  p3; p3 = p1+p2; p3.show(); system("PAUSE"); return EXIT_SUCCESS; }```