Implementing operator< on abstract base class
<p>I have a type hierarchy, and I'm not sure of a clean / good way to implement <code>operator&lt;</code> and <code>operator==</code>.</p>

<p>Essentially, I already have this:</p>

<pre><code>class Parent {
    public:
        virtual ~Parent() {}
};

class A : public Parent { int         data; };
class B : public Parent { double      data; };
class C : public Parent { std::string data; };

bool operator==(A const &amp; lhs, A const &amp; rhs) { return lhs.data == rhs.data; }
bool operator&lt; (A const &amp; lhs, A const &amp; rhs) { return lhs.data &lt;  rhs.data; }

bool operator==(B const &amp; lhs, B const &amp; rhs) { return lhs.data == rhs.data; }
bool operator&lt; (B const &amp; lhs, B const &amp; rhs) { return lhs.data &lt;  rhs.data; }

bool operator==(C const &amp; lhs, C const &amp; rhs) { return lhs.data == rhs.data; }
bool operator&lt; (C const &amp; lhs, C const &amp; rhs) { return lhs.data &lt;  rhs.data; }
</code></pre>

<p>What I'd like to implement as well, is this:</p>

<pre><code>bool operator==(Parent const &amp; lhs, Parent const &amp; rhs) { ... }
bool operator&lt; (Parent const &amp; lhs, Parent const &amp; rhs) { ... }
</code></pre>

<p>I've currently implemented it by doing:</p>

<pre><code>bool operator==(Parent const &amp; lhs, Parent const &amp; rhs) {
    try {
        return dynamic_cast&lt;A const &amp;&gt;(lhs) == dynamic_cast&lt;A const &amp;&gt;(rhs);
    } catch(std::bad_cast const &amp; e) {
    }

    try {
        return dynamic_cast&lt;B const &amp;&gt;(lhs) == dynamic_cast&lt;B const &amp;&gt;(rhs);
    } catch(std::bad_cast const &amp; e) {
    }

    try {
        return dynamic_cast&lt;C const &amp;&gt;(lhs) == dynamic_cast&lt;C const &amp;&gt;(rhs);
    } catch(std::bad_cast const &amp; e) {
    }

    assert(typeid(lhs) != typeid(rhs));
    return false;
}
</code></pre>

<p>But this just seems awful. Is there a cleaner way of going about this?</p>
