I’ve been searching around but can’t find much about this, but would cross-casting from one interface to another be considered bad design? Here is a sample of the code I’m using:
class IShip {
// strictly ship_like interface
// i.e. move, attack, dock, etc.
};
class Sim_object {
// all game objects are derived from this and represents component in composite pattern
// get_name()
// get_location()
// add
// remove
// etc.
};
template<typename T>
class Group : public Sim_object {
// composite functions
// add
// remove
// display
// map<T> container;
};
class Ship_group : public Group<IShip>, public IShip {
// added IShip functionality
};
class Ship : public Sim_object, public IShip {
// actual ship object
};
Anyway, I’m using MVC where my controller will manipulate IShip objects and depending on if they are composites or leafs, will perform some function. My question is at times I need to go from IShip to Sim_object to get a different interface (requiring a dynamic_cast). Would this be considered bad design/practice? I didn’t really want to pollute the IShip interface just to get access to the Sim_object commands.
Casting generally implies a bad design, unless the casted type is already known in that context. For example, if you have an interface IRenderer that draws textures represented by the interface ITexture and you have an implementation for OpenGL which has an OpenGLRenderer and an OpenGLTexture, casting the ITexture to OpenGLTexture in OpenGLRenderer wouldn’t be a design issue.
If you really need to cast the IShip to a Sim_object it would be reasonable to think that IShip should actually be a Sim_object.