RTTI and GetInterface()

Although run-time type information (RTTI) allows for querying polymorphic objects at run-time and safely casting down a class hierarchy, RTTI is not a replacement for the 3ds Max SDK GetInterface mechanism. RTTI improves code robustness, as illustrated by the following example that uses BaseInterface::GetInterface() with dynamic_cast, instead of Animatable::ClassID() with static_cast.

The following is the recommended way to cast an object to a particular interface:

template<typename Interface_T>
Interface_T* GetInterface(BaseInterface* base, Interface_ID id)
{
  if (base == NULL)
    return NULL;

  return dynamic_cast<Interface_T*>(base->GetInterface(id));
}

Animatable::Class_ID() and static_cast should only be used in the case where you need to retrieve a class that does not have an interface id.

template<typename Interface_T>
Interface_T* GetAnimInterface(Animatable* anim, Class_ID cid)
{
  if (anim == NULL)
    return NULL;

  if (anim->ClassID() != cid))
    return NULL;

  return static_cast<Interface_T*>(anim);
}

In addition, superclass ids (SClass_ID) and class ids (Class_ID) are necessary to create instances of objects based on type information using Interface::CreateInstance().