Thread: operator overloaeded inheritance

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    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:perator+(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;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.
    Last edited by grumpy; 01-12-2006 at 04:12 PM.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    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:perator+ to behave just like point:perator+.
    This is not what your point1:perator 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 !

  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  4. Inheritance and Polymorphism
    By bench386 in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2004, 10:19 PM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM