Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 25-10-2008
Member
 
Join Date: Oct 2008
Posts: 49
Virtual operator overloads don't seem to work

Code:
#include <iostream>

using namespace std;

class Base{
public:
virtual void Test(){
cout << "Base" << endl;
}

virtual bool operator==(const Base &other){
cout << "Base Comparison" << endl;
return false;
}
};

class Derived : public Base{
public:
void Test(){
cout << "Derived" << endl;
}

bool operator==(const Derived &other){
cout << "Derived Comparison" << endl;
return true;
}
};

int main(int argc, char** argv) {
Base a; //Create a base object
a.Test(); //Outputs "Base" as expected
Derived b, c; //Create two derived objects
b.Test(); //Outputs "Derived" as expected
if(b==c) cout << "True" << endl; //Does derived comparison and
returns true as expected.
Base *d=&b, *e=&c; //Create two base pointers to derived objects
d->Test(); //Outputs "Derived" as expected
if(*d==*e) cout << "True" << endl; //Does base comparison and
returns false!?
return 0;
}

The output is:

Base
Derived
Derived Comparison
True
Derived
Base Comparison

Notice, that the line "d->Test()" works correctly, but the comparison on the next line does not. The compiler (g++ (GCC) 4.2.3 (Ubuntu 4.2.3-2ubuntu7) ) seems to be ignoring the virtual-ness of Base:perator== . Is this correct? Have I made a mistake?
Reply With Quote
  #2  
Old 25-10-2008
Member
 
Join Date: Oct 2008
Posts: 7
Re: Virtual operator overloads don't seem to work

binary operators don't work very well with inheritance; what you'd really need to make them work is double dispatch. In the special case of == and !=, you can do something like:

bool
Derived:perator==( Base const& other ) const
{
Derived const* p
= dynamic_cast< Derived const* >( &other ) ;
return p != NULL && operator==( *p ) ;
}

in addition to your normal Derived:perator==, at least if you decide that two different derived types can never compare equal. If it makes sense for two different derived types to compare equal, however (say because they both represent the same value, but in different ways), you'll need to set up a much more complicated mechanism.
Reply With Quote
  #3  
Old 25-10-2008
Member
 
Join Date: Jul 2008
Posts: 20
Re: Virtual operator overloads don't seem to work

You can do that with a free function:

bool operator==(const Derived& Left, const Base& Right)
{ return Right.operator==(Left); }
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Virtual operator overloads don't seem to work"
Thread Thread Starter Forum Replies Last Post
Work with two separate domains and Virtual PC TheVi11ageIdiot Technology & Internet 1 17-05-2011 03:15 AM
Virtual Camera Operator for Ice Cream Sandwich Strong^Arm Portable Devices 4 16-05-2011 11:02 AM
Virtual PC doesnt work Harding Networking & Security 2 02-03-2009 12:35 PM
migrate Hyper-V virtual machine to virtual server/virtual PC 2007 Larry Scott Windows Server Help 1 18-03-2008 03:32 AM
virtual drives - don't work! DAEMON tools and more Adele2 Vista Help 2 08-03-2007 02:41 AM


All times are GMT +5.5. The time now is 10:42 AM.