When evaluating two objects (of the identical type), it seems sensible to possess a compare function that takes another instance of the identical class. Basically implement this like a virtual function within the base class, then your signature from the function needs to reference the bottom class in derived classes also. What's the elegant method to tackle this? If the Compare 't be virtual?
class A class B: A class C: A
I'd implement it such as this:
class A class B : A int A::Compare(A *other)let us just state that non-null > null */ if(a > other->a) return 1 if(a < other->a) return -1 return int B::Compare(A *other)
This is like the
IComparable pattern insInternet, which is effective.
One caveat towards the above is the fact that
a is definitely an A and
b is really a B) may return equality, and can never throw the best, whereas
b.Compare(a) will. Sometimes this is exactly what you would like, and often it isn't. When not, then you definitely most likely do not want your
Compare function to become virtual, or you need to compare
type_infos within the base
Compare function, as with:
int A::Compare(A *other)let us just state that non-null > null */ if(typeid(this) != typeid(other)) throw "Should be exactly the same type" if(a > other->a) return 1 if(a < other->a) return -1 return
Observe that derived classes'
Compare functions don't have to change, given that they should call the bottom class's
Compare, in which the
type_info comparison will occur. You are able to, however, replace the
dynamic_cast within the overridden
Compare function having a
Most likely, I'd get it done such as this:
class A amplifier rhs) const evaluations class B amplifier rhs) const amplifier b = dynamic_cast<A&lifier>(rhs) if (A::Compare(b) == /* equal */) evaluations else return /* not equal */ catch (std::bad_cast&lifier)