Call functions of derived classes in a list of base class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SaviorX
    New Member
    • Apr 2010
    • 1

    Call functions of derived classes in a list of base class

    So, I have created some Collision Detection classes that all derive from a base class known as CollisionBase.

    I have a List<CollisionB ase> in which I will be going through and calling a method known as Intersect. CollisionBase and all of the derived classes have this method defined, with the CollisionBase class having it as virtual and the defined classes having it as override.

    This list will contain up to three different derived classes in it (CollisionCircl e, CollisionRectan gle, CollisionLine), but I will not known until runtime what the list will contain. Is there anyway to "typecast" the function call Intersect to it calls the right function in the class that is currently being looked at in the list? I have the function I am using for this below:

    Code:
    foreach (CollisionBase c in collisionListVar)
    {
       if (c.Intersect(input))
       {
          return true;
       }
    }
Working...