Re: dynamic_cast is ugly!
dave_mikesell@fastmail.fm wrote:
std::vector<Drawable *> all_drawable_objects; // for the render loop
std::vector<Collidable *> all_collidable_objects; // for collision
detection
I only care about Collidables during collision detection. No need to
iterate over all objects and interrogate the type of each.
In my case this solution doesn't work for two reasons:
1) The order of the objects may change in the main container (and this
ordering is very relevant). I would have to maintain the same order in
the type-specific containers as well, which in some cases can become
exceedingly difficult. (Certainly more difficult than having to use
dynamic cast in a few places, so trying to do it becomes
counter-productive.)
2) In some cases I have to traverse *all* the objects in the order in
which they are in the main container and perform *type-specific*
operations to them (iow. operations which cannot be specified as virtual
functions in the base class). It would not be enough to traverse the
type-specific containers one after another (because it would mean that
the objects are traversed out-of-order with respect to the main container).
I actually am using your solution where I can (if for nothing else,
because it's more efficient, as less objects need to be traversed).
However, I can't do it in all cases.