Safe Type-casting
 
 
 

Type-casting a pointer from a derived class to a base class is inherent to C++. Type-casting a pointer from a base class to a derived class is not safe and could possibly point to the incorrect data. The API provides methods to achieve this “down-casting”. The example programs, especially the DAG traversal functions, show how down-casting is used.

For example, if class derivation is AlObject->AlLight->AlAmbientLight, where the AlObject class is the base class, and AlAmbientLight is the most derived class, the following code shows how down-casting is done:

AlAmbientLight *ambientLightPtr = objectPtr->asAmbientLightPtr(); 
if (ambientLightPtr != NULL) {
 	// then we know the object is an ambient light 
}

You do not have to first cast the object to an AlLight. Also note that a new wrapper is not allocated when using the casting methods. In this way these methods can be thought of as regular casts.