Assuming that the runtime type information (RTTI) is enabled, you can cast the pointer to B*
using dynamic_cast
, and see if you get a non-null value back:
A* ptr = ... // some pointer
if (dynamic_cast<B*>(ptr)) {
// ptr points to an object of type B or any type derived from B.
}
Another way of doing this would be using typeid
:
if (typeid(*ptr) == typeid(B)) {
// ptr points to an object of type B, but not to a type derived from B.
}
Note: if you need to do this often, good chances are that your design can be improved.