7

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?

4
  • 2
    "but when I try to run it, it just hangs" - post code. Mar 31, 2015 at 23:07
  • @vsoftco So, what you are suggesting is that only Base is comparable, but not any derived classes?
    – Ælex
    Mar 31, 2015 at 23:12
  • @Alex, not at all, the operator== is inherited, and if the getters are virtual, then you're good to go.
    – vsoftco
    Mar 31, 2015 at 23:13
  • @Alex, what I'm saying is that the hierarchy should be comparable via a public Base interface, otherwise the design is not the best (imho). When you talk about inheritance, the idiom to keep in mind is that Derived is always also a Base. So in principle Rectangle is not a Square, even if you're first tempted to write it as a derived class.
    – vsoftco
    Mar 31, 2015 at 23:32

2 Answers 2

13

I agree with @vsoftco about only implementing operator== in the base class and using the NVI idiom. However, I'd provide a pure virtual function that the derived classes need to implement to perform the equality check. In this way the base class doesn't know or care about what it means for any derived classes to be equivalent.

Code

#include <iostream>
#include <string>
#include <typeinfo>

class Base
{
public:
    virtual ~Base() {}

    bool operator==(const Base& other) const
    {
        // If the derived types are the same then compare them
        return typeid(*this) == typeid(other) && isEqual(other);
    }

private:
    // A pure virtual function derived classes must implement.
    // Furthermore, this function has a precondition that it will only
    // be called when the 'other' is the same type as the instance
    // invoking the function.
    virtual bool isEqual(const Base& other) const = 0;
};

class D1 : public Base
{
public:
    explicit D1(double v = 0.0) : mValue(v) {}
    virtual ~D1() override {}

private:
    virtual bool isEqual(const Base& other) const
    {
        // The cast is safe because of the precondition documented in the
        // base class
        return mValue == static_cast<const D1&>(other).mValue;
    }

private:
    double mValue;
};

class D2 : public Base
{
public:
    explicit D2(std::string v = "") : mValue(v) {}
    virtual ~D2() override {}

private:
    virtual bool isEqual(const Base& other) const
    {
        return mValue == static_cast<const D2&>(other).mValue;
    }

private:
    std::string mValue;
};

class D3 : public Base
{
public:
    explicit D3(int v = 0) : mValue(v) {}
    virtual ~D3() override {}

private:
    virtual bool isEqual(const Base& other) const
    {
        return mValue == static_cast<const D3&>(other).mValue;
    }

private:
    int mValue;
};

int main()
{
    D1 d1a(1.0);
    D1 d1b(2.0);
    D1 d1c(1.0);

    D2 d2a("1");
    D2 d2b("2");
    D2 d2c("1");

    D3 d3a(1);
    D3 d3b(2);
    D3 d3c(1);

    std::cout << "Compare D1 types\n";
    std::cout << std::boolalpha << (d1a == d1b) << "\n";
    std::cout << std::boolalpha << (d1b == d1c) << "\n";
    std::cout << std::boolalpha << (d1a == d1c) << "\n";

    std::cout << "Compare D2 types\n";
    std::cout << std::boolalpha << (d2a == d2b) << "\n";
    std::cout << std::boolalpha << (d2b == d2c) << "\n";
    std::cout << std::boolalpha << (d2a == d2c) << "\n";

    std::cout << "Compare D3 types\n";
    std::cout << std::boolalpha << (d3a == d3b) << "\n";
    std::cout << std::boolalpha << (d3b == d3c) << "\n";
    std::cout << std::boolalpha << (d3a == d3c) << "\n";

    std::cout << "Compare mixed derived types\n";
    std::cout << std::boolalpha << (d1a == d2a) << "\n";
    std::cout << std::boolalpha << (d2a == d3a) << "\n";
    std::cout << std::boolalpha << (d1a == d3a) << "\n";
    std::cout << std::boolalpha << (d1b == d2b) << "\n";
    std::cout << std::boolalpha << (d2b == d3b) << "\n";
    std::cout << std::boolalpha << (d1b == d3b) << "\n";
    std::cout << std::boolalpha << (d1c == d2c) << "\n";
    std::cout << std::boolalpha << (d2c == d3c) << "\n";
    std::cout << std::boolalpha << (d1c == d3c) << "\n";

    return 0;
}

Output

Compare D1 types
false
false
true
Compare D2 types
false
false
true
Compare D3 types
false
false
true
Compare mixed derived types
false
false
false
false
false
false
false
false
false
3
  • That is actually what I had hoped for at first. Very smart, I'll see if I can use it, thank you very much.
    – Ælex
    Apr 1, 2015 at 5:40
  • Thank you very much, this is by far the most elegant thing I've seen so far, kudos! I'm not sure I understand how the precondition works, but I'll google it!
    – Ælex
    Apr 2, 2015 at 20:08
  • 1
    @Alex The precondition is just documenting a requirement that must be obeyed for things to work correctly. It is the caller's responsibility (i.e., Base::operator==) to make sure the precondition is met. This is similar to the precondition on std::vector<T>::operator[](index) which has a precondition that index is not out of bounds otherwise there is undefined behavior. Apr 2, 2015 at 20:17
1

In general, in a hierarchy, one should have a common interface, and imo the operator== should be only implemented in the Base class using (virtual) getters from the interface. Otherwise it's like re-defining functions (without using virtual) down the hierarchy, which is almost always a bad idea. So you may want to think about your design, having more than one operator== seems fishy.

Very simple example:

#include <iostream>

class A
{
    int _x;
public:
    A(int x):_x(x){}
    virtual int getx() const { return _x; } // runtime
    bool operator==(const A& other){return getx() == other.getx();} // one implementation
};

class B: public A
{
    using A::A;
    int getx() const override // Make all B's equal, runtime
    {
        return 0; // so always 0, all B's are equal
    }
};

int main()
{
    A a1(10), a2(20);
    B b1(10), b2(20);
    std::cout << std::boolalpha << (a1==a2) << std::endl; // false
    std::cout << std::boolalpha << (b1==b2) << std::endl; // always true
}

This pattern is usually called the non-virtual interface idiom and is a manifestation of the so called template method (has nothing to do with templates, just an unfortunate name), in which you have clients (along the hierarchy) call virtual functions indirectly through public non-virtual member functions. Item 55 of Scott Meyers' Effective C++ has an excellent discussion about this issue.

3
  • Thanks a lot vsoftcol, I'll take a look and see how I can make this better, as I'm unhappy with my current code!
    – Ælex
    Mar 31, 2015 at 23:32
  • @Alex no problem, hope it helps.
    – vsoftco
    Mar 31, 2015 at 23:33
  • @Alex I agree with vsoftco on many points. However, I provided a solution with a slight difference, it doesn't involve virtual getters. Instead, it uses the NVI idiom and encapsulates the logic of the comparison in the derived classes. Apr 1, 2015 at 4:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.