Original Post
Hello all, I have some questions regarding the design of OOP programs. Right now my program is written in Java and uses the instanceof operator. I have heard that there is a lot of taboo about this, mostly that using it is generally bad design. I am now moving this program over to C++ and since C++ doesn't have instanceof I want to design the code better. Right now, I have a game that has a lot of different objects that are stored in my World class. I have a lot of different interfaces such as Logicable, Drawable, Collidable, etc. etc.. Each object is added to a specific list where the methods are then handled by my World class. If this is confusing allow me to show you guys an example of where I use instanceof...
public static void addObject(Object o)
{
if(o instanceof Collidable)
{
staticCollisionList.add((Collidable)o);
}
if(o instanceof SortedDrawable)
{
sortedDrawableList.add((SortedDrawable)o);
}
if(o instanceof Logicable)
{
logicableList.add((Logicable)o);
}
}
Is this bad design? I am not sure how else I could achieve such ease in Java. Classes themselves are created, implement the interfaces they need, and then objects of the class can be added to the World using the above function and immediately run. Additionally if I want to achieve this in C++ how should I go about doing it? I am aware C++ uses multiple inheritance instead of interfaces. But what is the best method of storing and running objects which may have shared attributes. For example, if I had an Actor (updates, draws, and collides) and a Light (updates, draws), and a CollisionBox (draws, and collides) how should I keep track of and handle these? Thanks.