Sort objects and polymorphism
- by ritmbo
Suppose I have a class A.
And B and C are child of A.
Class A has a generic algorithm for sorting arrays of type A, so that I could use it for B and C without writing again the algorithm for each.
In the algorithm, sometimes I have to swap.
The problem is that I can only see the objects as type A, and if I do:
A aux = array[i]
array[i] = array[j]
array[j] = aux
I think I have a problem. Because array[i], maybe its of type B, and aux is of type A, so I think I'm losing information.
I'm sure u understand this situation... how can I sort a generic array of objects using a father method algorithm?