You are here:

C++/dynamic binding of overloaded operator

Advertisement


Question
Hi,
I tried the code you suggested under g++ compiler in Solaris and Linux OS both. There is no polymorhic behaviour.

#include<iostream>
using namespace std;
class base
 {
       public:
       virtual void operator + (base & i) { cout<<"base operator  
         called\n";}
 };
class derived : public base
 {
       public:
        void operator + (derived & i) { cout<<"derived operator
         called\n";}
};

main()
{
base *bp1=new base;
base  *bp2 = new derived;
base b1;
derived d1;

*bp1+b1;
*bp2+d1;

}

ouput:
base operator called
base operator called


-------------------------
Followup To
Question -
thanks for replying. I have changed your program and pasted below this time it is not showing polymorhic behaviour.

#include<iostream>
class base
 {
       public:
       virtual void operator + (base i) { cout<<"base operator called\n";}
 };
class derived : public base
 {
       public:
        void operator + (derived i) { cout<<"derived operator called\n";}
};

main()
{
base *bp1=new base;
base  *bp2 = new derived;
base b1;
derived d1;

*bp1+b1;   //callling base op
*bp2+d1;   //this also calls base op

}


-------------------------
Followup To
Question -
Is it possible to achieve dynamic binding for overloaded operator?
I have written a program to test it but the base pointer (though assigned to derived object) is always calling base's overloaded operator.


Answer -
Yes, it is. Overloaded operators are just syntactic sugar for regular functions. They have all of the properties of regular functions, including polymorphic behavior:

#include <iostream>

using namespace std;

class Base {
public:
   virtual void operator+(int i) { cout<<"Base operator\n"; }
};

class Derived: public Base {
public:
   void operator+(int i) { cout<<"Derived operator\n"; }
};

int main()
{
   Base *a = new Base;
   Base *b = new Derived;

   *a + 1; // Calls Base::operator+
   *b + 1; // Calls Derived::operator+

   delete a;
   delete b;
}

-Sandra
Answer -
In C++, virtual functions are allowed to differ only in the return type. This is called covariance, and allows you to do things like this:

virtual base& f(); // base class
derived& f() // derived class

The same relaxation of the rules breaks the type system and really isn't a necessary feature, so it isn't provided. The following code does what you want, but also has a problem:

#include<iostream>

using namespace std;

class base
{
public:
   virtual void operator + (base i) { cout<<"base operator called\n";}
};

class derived : public base
{
public:
   void operator + (base i) { cout<<"derived operator called\n";}
};

main()
{
   base *bp1 = new base;
   base *bp2 = new derived;
   base b1;
   derived d1;

   *bp1+b1;   //callling base op
   *bp2+d1;   //this also calls base op
}

The problem is that you pass a base object by value in the derived class' overridden function. This causes slicing to occur. Anything you added to derived will not be available to you since the function only sees a base object. To avoid slicing, pass either a pointer or reference to the base class object. That way you can get polymorphic behavior with the parameter:

class base
{
public:
   virtual void operator + (base& i) { cout<<"base operator called\n";}
};

class derived : public base
{
public:
   void operator + (base& i) { cout<<"derived operator called\n";}
};

-Sandra

Answer
>There is no polymorhic behaviour.
Because you're still making the same mistake.

>operator + (base & i)
>operator + (derived & i)
The parameter types for overridden member functions must be exactly the same. Your code does not override the virtual function because the parameter is a reference to a derived instead of a reference to a base. Copy the following code exactly as it is and you'll have no problems.

#include<iostream>

using namespace std;

class base
{
public:
   virtual void operator + (base & i)
   { cout<<"base operator called\n";}
};

class derived : public base
{
public:
   void operator + (base & i)
   { cout<<"derived operator called\n";}
};

int main()
{
   base *bp1 = new base;
   base  *bp2 = new derived;
   base b1;
   derived d1;

   *bp1+b1;
   *bp2+d1;
}

-Sandra

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Sandra Guth

Expertise

I can help with any questions concerning ISO standard C++, object oriented programming and design as well as algorithms and data structures.

Experience

I have nine years of programming experience with C++.

©2016 About.com. All rights reserved.