Operator== in derived class never gets called.
Posted
by Robin Welch
on Stack Overflow
See other posts from Stack Overflow
or by Robin Welch
Published on 2010-01-26T23:07:17Z
Indexed on
2010/04/10
18:33 UTC
Read the original article
Hit count: 304
Can someone please put me out of my misery with this? I'm trying to figure out why a derived operator== never gets called in a loop. To simplify the example, here's my Base and Derived class:
class Base { // ... snipped
bool operator==( const Base& other ) const { return name_ == other.name_; }
};
class Derived : public Base { // ... snipped
bool operator==( const Derived& other ) const {
return ( static_cast<const Base&>( *this ) ==
static_cast<const Base&>( other ) ? age_ == other.age_ :
false );
};
Now when I instantiate and compare like this ...
Derived p1("Sarah", 42);
Derived p2("Sarah", 42);
bool z = ( p1 == p2 );
... all is fine. Here the operator== from Derived gets called, but when I loop over a list, comparing items in a list of pointers to Base objects ...
list<Base*> coll;
coll.push_back( new Base("fred") );
coll.push_back( new Derived("sarah", 42) );
// ... snipped
// Get two items from the list.
Base& obj1 = **itr;
Base& obj2 = **itr2;
cout << obj1.asString() << " " << ( ( obj1 == obj2 ) ? "==" : "!=" ) << " "
<< obj2.asString() << endl;
Here asString()
(which is virtual and not shown here for brevity) works fine, but obj1 == obj2
always calls the Base
operator==
even if the two objects are Derived
.
I know I'm going to kick myself when I find out what's wrong, but if someone could let me down gently it would be much appreciated.
© Stack Overflow or respective owner