Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iterate through all the instances of base class and inherited classes

I have a base class named CollidableObject and a couple of inherited classes named Player, Enemy, InertObject, etc.

I am trying to find an easy way to iterate through all the instances of them, so I initially created a list of the type CollidableObject and put all the instances of the inherited classes in there.

The thing is that because of the nature of polymorphism, when I do the following

foreach (CollidableObject CollidableObject in collidableObjects)
{
  if (CollidableObject is Player)
  {
    CollidableObject.Draw(spriteBatch, testPlayerTexture); 
  }
  // Then the same prodedure for each type of CollidableObject. 
  // Might change to switch or something.                              
}

The draw method that it calls is the generic Draw method from the CollidbaleObject base, not the overridden/new one from the Player/Enemy/inertObject class.

How do I get around this. Is there a way to iterate through a collection of objects from the same tree but maintaining their inherited type?

like image 915
user3120072 Avatar asked Dec 19 '13 17:12

user3120072


People also ask

How do you access an inherited class from base class?

To access an element of a base class from an inherited class, you need to use the base keyword. The base keyword is used in inherited classes in the following cases: when from the constructor of a derived class you need to call the constructor of the base class.

What all is inherited from the base class?

1) The base class's constructors and destructor. 2) The base class's friend functions. 3) Overloaded operators of the base class.

Can base classes inherit from another base class?

Classes derived from a base class are called child classes, subclasses or derived classes. A base class does not inherit from any other class and is considered parent of a derived class.

Can a class inherit from multiple base classes?

You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called multiple inheritance. The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by destructors.


2 Answers

Is there no way to iterate through a collection of objects from the same tree but maintaining their inherited type?

Absolutely, there is a way to do that. However, you need to set up your hierarchy in a proper way:

  • The Draw method in the CollidableObject needs to be marked virtual
  • The Draw method in the Player needs to be marked as an override.

This will ensure that the call from your post would be routed to the Player's override of the method, not to the method of the base.

On a related note, when you see code that checks the dynamic type of an object with the is operator, as in if (CollidableObject is Player), you should get strong suspicions that you are doing something incorrectly. For example, you may be missing a double dispatch.

If all you need to know is the proper texture for the type, you could put textures in a Dictionary<Type,Texture> textureForType, and pull the right one in your loop using the GetType() of the collidable object.

like image 173
Sergey Kalinichenko Avatar answered Nov 15 '22 09:11

Sergey Kalinichenko


Have you tried:

foreach (CollidableObject CollidableObject in collidableObjects)
{
    if (CollidableObject is Player)
    {
        ((Player) CollidableObject).Draw(spriteBatch, testPlayerTexture); 
    }    
    //Then the same prodedure for each type of CollidableObject. Might change to switch or something.                              
}
like image 25
ryudice Avatar answered Nov 15 '22 09:11

ryudice