Results 1 to 3 of 3

Thread: Virtual operator overloads don't seem to work

  1. #1
    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?

  2. #2
    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.

  3. #3
    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); }

Similar Threads

  1. Work with two separate domains and Virtual PC
    By TheVi11ageIdiot in forum Technology & Internet
    Replies: 1
    Last Post: 17-05-2011, 03:15 AM
  2. Virtual Camera Operator for Ice Cream Sandwich
    By Strong^Arm in forum Portable Devices
    Replies: 4
    Last Post: 16-05-2011, 11:02 AM
  3. Virtual PC doesnt work
    By Harding in forum Networking & Security
    Replies: 2
    Last Post: 02-03-2009, 12:35 PM
  4. migrate Hyper-V virtual machine to virtual server/virtual PC 2007
    By Larry Scott in forum Windows Server Help
    Replies: 1
    Last Post: 18-03-2008, 03:32 AM
  5. Replies: 2
    Last Post: 08-03-2007, 02:41 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,167,925.25969 seconds with 17 queries