Skip navigation

Monthly Archives: October 2013

C++’s RTTI is expensive. Very expensive. Notoriously and verifiably expensive.

So how can we efficiently get information of the concrete subclass, given an abstract base class pointer, without the great expense of using RTTI?

Simple. Use an enum.

Consider the example in my stackoverflow post. What I did there is just keep a single bitfield (an int) that has concrete types bitwise OR’d in, in each derived constructor.

So, in the example posted, an object of Type BigCat would have 0111 for its typeTag field, indicating that it is an Animal, a Cat, and a BigCat. Concrete type checking is cheap as a bitwise AND.

Advertisements

eta is easy to compute if you have distance between 2 objects, their relative accelerations, and relative velocities.

By _relative_ accelerations and velocities, i mean the rate at which they are coming together. So you have to project both objects’ velocity components onto the line joining the two objects, and use that scalar component in these computations.

Without further ado, eta:

eta = -v1 +/- sqrt( v12 + 2a(dist) ) / a

In image: