Let's say I have a base abstract class B
and a hundred classes which derive from it D1
... D100
. I also have two (smart) pointers unique_ptr<B> p1, p2;
which point to two different instances of types Di
and Dj
. I want to know whether the objects they're pointing to have the same type (i.e. whether i
equals j
). Is there a simple way to do that?
You can use a RTTI typeid, but generally, it is a bad design to must have to use dynamic_cast because it may violate the liskov substitution principle
std::unique_ptr<B> p1, p2;
if(typeid(*p1) == typeid(*p2)) // correction by Justin
Or something like that using name
or hash_code
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With