I am trying to wrap my head around something I've been wondering for quite some time now.
Assume I have a class Base
class Base
{
public:
virtual ~Base(){}
virtual bool operator== ( const Base & rhs ) const;
};
Now, another class inherits from it. It has two equality operators:
class A : public Base
{
public:
bool operator== ( const A & rhs ) const;
bool operator== ( const Base & rhs ) const;
private:
int index__;
};
And yet another class which also inherits from Base, and which also has two equality operators:
class B : public Base
{
public:
bool operator== ( const B & rhs ) const;
bool operator== ( const Base & rhs ) const;
private:
int index__;
};
This is what I understand (which is not necessarily correct). I can use the first operator only to check if same class objects are equal. Yet, I can use the second operator to check if they are the same type of class, and then if they are equal. Now, yet another class exists, which wraps around pointers of Base, which are however, polymorphic types A, or B.
class Z
{
public:
bool operator== ( const Z & rhs ) const;
private:
std::shared_ptr<Base> ptr__;
};
First things first, I've found out, that I can't have two operator== overloaded. I get no errors from the compiler, but when I try to run it, it just hangs. I am guessing it has something to do with rtti, which is beyond me.
What I have been using, and is quite ugly, is attempting to downcast, and if I can, then try to compare instances, within class Z:
bool Z::operator== ( const Z & rhs ) const
{
if ( const auto a1 = std::dynamic_pointer_cast<A>( this->ptr__ ) )
if ( const auto a2 = std::dynamic_pointer_cast<A>( rhs.ptr__ ) )
return *a1 == *a2;
else if ( const auto b1 = std::dynamic_pointer_cast<B>( this->ptr__ ) )
if ( const auto b2 = std::dynamic_pointer_cast<B>( rhs.ptr__ ) )
return *b1 == *b2;
return false;
}
This is quite ugly, and it assumes that your class A and B, have an equality operator which takes as parameter the same type class.
So I tried to come up with a way, which would use the second type of operator, more agnostic, more elegant if you will. And failed. This would require to use it in both classes A and B, thus moving it away from class Z.
bool A::operator== ( const Base & rhs ) const
{
return ( typeid( *this ) == typeid( rhs ) ) && ( *this == rhs );
}
Same for class B. This doesn't seem to work (app hangs without any errors). Furthermore, it uses some kind of default operator, or does it use the base class operator? Ideally, it should use both the Base::operator== and compare class types.
If however, I want a more elaborate comparison, based upon a member of class A, or B, such as index__
then I obviously have to friend each class, because when I try this, it won't compile (unless of course I add a getter or make it somehow visible):
bool A::operator== ( const Base & rhs ) const
{
return ( typeid( *this ) == typeid( rhs ) )
&& (*this == *rhs )
&& (this->index__ == rhs.index__ );
}
Is there an elegant, simple solution to this? Am I confined to downcasting and trying, or is there some other way to achieve what I want?
operator==
is inherited, and if the getters arevirtual
, then you're good to go.Base
interface, otherwise the design is not the best (imho). When you talk about inheritance, the idiom to keep in mind is thatDerived
is always also aBase
. So in principleRectangle
is not aSquare
, even if you're first tempted to write it as a derived class.