Assume we have three classes A, B and C. B is derived from A and C is derived from B. Now we have a pointer that points to an object of class A. Due to Polymorphism it can actually point to instances of all three classes.
With typeid() i can check what type the pointer actually refers to. But I'm trying to determine if it points to any descendant of class B. That is to say I'm looking for some kind of IsDescendantOf(unkownclass, baseclass) function. Is there a why to do so in C++?
Use dynamic_cast
. It returns NULL on failure:
B* pb = dynamic_cast<B*>(pa);
You may find this MSDN article helpful.
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