This post has already been read 2977 times!

Problem

You have two objects, and you need to know if their respective classes have a base class/derived class relationship or if they are unrelated.

Solution

Use the dynamic_cast operator to attempt to downcast from one type to another. The result tells you about the class’s relationships.

Example presents some code for doing this.
Determining class relationships

#include <iostream>
#include <typeinfo>
using namespace std;
class Base {
public:
virtual ~Base( ) {} // Make this a polymorphic class
};
class Derived : public Base {
public:
virtual ~Derived( ) {}
};
int main( ) {
Derived d;
// Query the type relationship
if (dynamic_cast<Base*>(&d)) {
cout << "Derived is a subclass of Base" << endl;
}
else {
cout << "Derived is NOT a subclass of Base" << endl;
}
}

Use the dynamic_cast operator to query the relationship between two types. dynamic_cast takes a pointer or reference to a given type and tries to convert it to a pointer or reference of a derived type, i.e., casting down a class hierarchy. If you have a Base* that points to a Derived object, dynamic_cast<Base*>(&d) returns a pointer of type Derived only if d is an object of a type that’s derived from Base. If this is not possible (because Derived is not a subclass, directly or indirectly, of Base), the cast fails and NULL is returned if you passed dynamic_cast a pointer to a derived
object. If it is a reference, then the standard exception bad_cast is thrown. Also, the base class must be publicly inherited and it must be unambiguous. The result tells you if one class is a descendant of another.

Here’s what I did in Example:

if (dynamic_cast<Base*>(&d)) {
This returns a non-NULL pointer because d is an object of a class that is a descendant of Base. Use this on any pair of classes to determine their relationship. The only requirement is that the object argument is a polymorphic type, which means that it has at least one virtual function. If it does not, it won’t compile. This doesn’t usually cause much of a headache though, because a class hierarchy without virtual func-tions is uncommon.

If the syntax is too messy for you, you can use a macro to hide some of the details:

#define IS_DERIVED_FROM(baseClass, x) (dynamic_cast<baseClass*>(&(x)))
//...
if (IS_DERIVED_FROM(Base, l)) { // ...

This type information is not free, though, because dynamic_cast must traverse the class hierarchy at runtime to determine if one class is a descendant of another, so be smart about where you use it. Additionally, compilers don’t include this information by default since there is overhead required for RTTI, and not everyone uses this feature, so you may have to enable it with a compiler switch.

Comments are closed.

Post Navigation