Sort objects and polymorphism

Posted by ritmbo on Stack Overflow See other posts from Stack Overflow or by ritmbo
Published on 2010-03-17T19:36:31Z Indexed on 2010/03/17 19:41 UTC
Read the original article Hit count: 162

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about c++

Related posts about sort