I have Base* pointers to two instances of a polymorphic type and I need to determine if the referenced objects are equivalent.
My current approach is to first use RTTI to check for type equality. If the types are equal, I then call a virtual is_equivalent function.
Is there a more idiomatic approach?
In C++ this is called 'equality' and is usually implemented using
operator==()
. In C++ you can override the meaning of operators, it is possible to write:And have
==
call a custom function you define.I think you want to differentiate equality from identity which would mean the same object (i.e. same address).
You can implement it as member function or free function (from wikipedia):
In your case you want to implement
operator==
for the base class, and then perform what you are doing.More concretely it would look like this: