0

I try to have an operator== called from a derived class

but if i use base class pointers, it does not work, although the objects are created as derived classes

class A 
{
public:
    bool operator==(const A & other)
    {
        cout << "this should never been called" << endl;
        return this==&other;
    }
};

class B : public A
{
public:
    bool operator==(const A & other)
    {
        cout << "this should be called instead" << endl;
        return this==&other;
    }
};

void process(A _b1,A _b2)
{
    if(_b1==_b2)
    {
       cout << "here" << endl;
    }
}

int main() 
{
   B b1;
   B b2;

   process(b1,b2);
}

although the operators overload have the same signature, i dont get it

plz help me out

thx

Phil
  • 708
  • 1
  • 11
  • 22
  • for some reason the Hello at the beginning of my thread don't show up so.. Hello :-) – Phil Apr 05 '14 at 20:49
  • It does not really make sense to compare the memory address in the equality operator. You can just do that by default without implementing operator==. – Coert Metz Apr 05 '14 at 20:54
  • By passing b1 and b2 to process they are implicitly casted to type A (the type of the arguments of process). – Coert Metz Apr 05 '14 at 20:55
  • 1
    You need to deal with the slicing problem *and* use the `virtual` keyword. Note that a virtual `==` operator that actually makes any sense is extremely tricky, as are all other "binary" operators. – n. m. could be an AI Apr 05 '14 at 20:57
  • no slicing here, besides it works with other OO languages but i a m not sure if it is my code wich is wrong or acpp limitation – Phil Apr 05 '14 at 21:00
  • C++ is not Java or C#. You *are* encountering slicing problems here. – David G Apr 05 '14 at 21:02
  • ok but even c++ should call the most specialised operator – Phil Apr 05 '14 at 21:04
  • i added virtual to the A class operator but still not working – Phil Apr 05 '14 at 21:04

2 Answers2

1

First, the bool operator==() in the class A should be virtual. Like:

class A 
{
public:
    virtual bool operator==(const A& other) { ... }
};

That means that even if you refer to a class A, it can be treated as B, if that is what it really is.

Second, you make a copy of your B instances to A instances. That is a conversion you do not want. Instead you should pass them "by reference". Like this:

void process(A& _b1,A& _b2)
{
    ...
}

That way, when they are actually processed, they still refer to the original objects, which are of class B.

0

The problem is void process(A _b1,A _b2), it makes an implicit conversion/upcasting of the B class in A class, so it is called the A operator==, you should change your code to support polymorphism:

class A 
{
public:
    virtual bool operator==(const A & other)
    {
        cout << "this should never been called" << endl;
        return this==&other;
    }
};

class B : public A
{
public:
    virtual bool operator==(const A & other)
    {
        cout << "this should be called instead" << endl;
        return this==&other;
    }
};

void process(A& _b1,A& _b2)
{
    if(_b1==_b2)
    {
       cout << "here" << endl;
    }
}

int main() 
{
   B b1;
   B b2;

   process(b1,b2);
}

Ciao Angelo

AngeloDM
  • 397
  • 1
  • 8