Safely Downcast Pointers Without dynamic_cast Overhead

Suppose you have a class hierarchy and you plan to do some downcasts. Try writing virtuals in the root class to do the downcasting to NULL for each of the subclasses. Then, write each subclass so it downcasts to itself statically. I run Debian and gcc 3.4.3 on a modern Pentium, and I ran this test with optimization turned off (the loops might have disappeared with optimization turned on). In this example, the total runtime is eight times faster using the virtuals than dynamic_cast. Here's an example: